File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes casting int from session variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "casting int from session variable" Watch "casting int from session variable" New topic

casting int from session variable

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 751
Hi, I have this statement in my Servlet

session.setAttribute("errorflag", 0);

I'm having problem casting it to int like...

int errorFlag = (int) session.getAttribute("errorflag", 0);

any idea on how to solve this? Thanks!
[ October 27, 2005: Message edited by: Timothy Sam ]

SCJP 1.5
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63857

session.setAttribute("errorflag", 0);

This will not compile under Java 1.4. The second parameter must be an object. If you are using Java 1.5, this may be ok since the 0 may auto-box to an Integer. I'm not sure since I'm still using 1.4.

int errorFlag = (int) session.getAttribute("errorflag", 0);

First, getAttribute() does not take a second parameter.

Second, since you cannot cast anything to something that it is not, you cannot cast the return value of type Object to int, since int is not an Object. If an Integer is being returned, you could use the intValue() method to obtain the int value of the Integer.

Again, I'm not certain what the auto-boxing behavior of Java 1.5 would be -- it could be that your only issue is the spurious 2nd parameter.
[ October 27, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: casting int from session variable
It's not a secret anymore!