• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic