aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Autoboxing and initialization of non-instance variables (Need cameron's permission) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Autoboxing and initialization of non-instance variables (Need cameron Watch "Autoboxing and initialization of non-instance variables (Need cameron New topic
Author

Autoboxing and initialization of non-instance variables (Need cameron's permission)

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
In the vein of Marc's "SJCA candidates missing out?" post I'd like some help with some content on the exam. There's a question in the "Java 1.5" area of Cameron's mock exam book that I can't understand.

I'd like to actually post the question but as I don't have any right to reproduce it I'll wait till Cameron pops by and give me the
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Normally it's okay to post a limited excerpt (like a singe question), as long as the source is cited. But getting permission first is never a bad idea.

In the meantime, can you phrase your questions in general terms?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
Ok, here's the general jist of it. Theres a question that asks which of the following are valid ways of initializing a non-instance variable and the answers are like these:

int i = 10;
int i = new Integer(10);

When I looked at the possible answers I thought he must have made a typo putting "non-" in front of "instance." A non-instance variable must have the word static in it's declaration right? Maybe what he means is that the variable declaration could be sitting inside a static method and therefor the variable would be considered non-instance?
I dun get it.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

My guess is this question is about "local variables."

A local variable is a variable that's declared within a narrower scope, like the body of a method. For example, in the following code, x is an instance variable, and its scope is the entire class. But y is a variable that's local to the method, and it has no meaning outside of the method body.

The instance variable x gets an automatic default value of zero. But unlike instance variables, local variables are not automatically initialized. So you must explicitly value them before they are used.

Does that made sense?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Regarding autoboxing...

Basically, if the context requires a wrapper instance (like Integer), you can use a primitive (like int), and Java will automatically handle the conversion -- boxing the int to an Integer. Conversely, if the context requires a primitive, you can use a wrapper instance, and Java will automatically unbox.

In the example below (which uses a local variable inside main), a new Integer object is created. But to be assigned to an int variable, it is automatically converted (unboxed) to type int.
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
One of the incorrect options in this question was like this:

int i;

So my guess, Marc, is that you were right about him meaning local variables by non-instance variables. I wasn't aware that local variables weren't initialized to a default value like instance variables were. Thanks!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tristan Rouse:
One of the incorrect options in this question was like this:

int i;...

You can declare a local variable without giving it a value, but you need to give it a value before using it. For example...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing and initialization of non-instance variables (Need cameron's permission)
 
Similar Threads
Tips for SCJA
Obsolete Design Patterns for EJB3 & SCEA 5
SCJA study material
Sun Certified Java Associate
SCJA exam strategy "will it compile" vs. "follows naming convention"