Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic