• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

instance as an object

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a Lotus Notes Developer breaking into OO. I am reading Head First Java 5.0 and am on chapter 2. Clarification please on the topic of instance variables� Is an instance variable an Object? Am I correct in saying instance variables are Objects and since a �class� is the blueprint of an Object instance variables are Objects within the main Object type created from the class? Whew, I think I need a drink!
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables are variables declared within the class, but outside of any methods within the class. These variables (they can be objects or primitive data types), are created whenever a class is instantiated. Each instantiation of a class results in an instance of that class and a new set of the variables.

The fact that each instance of the class has it's own set of variables is what allows each instance of the class to have it's own unique state. One instance of a class can modify one of these variables and it does not affect any other class instance.

If the class level variables are declared with the keyword static, then the variables are created only one time when the class is loaded. Each instance of the class then shares the single copy of the variable. If one instace of a class modifies this variable then all instances of the class see the change.

To sum up, instance variable are unique objects or primitivesand are created and initialized when an object of the class is created (instantiated).
 
John Mateer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom,

your snippet in the "To sum up" section is exactly what I needed. I'm adding it to my book now.

Thank you,
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Blough:
To sum up, instance variable are unique objects or primitivesand are created and initialized when an object of the class is created (instantiated).



Careful: variables *are* never objects, they are either primitives or *references to* objects.
 
John Mateer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since I am very very new to Java is this what you mean... (see quotes)

To sum up, instance variables can be unique "object references" or primitives and are created and initialized when an object of the class is created (instantiated).
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup. I might stick with "initialized" instead of "created and initialized". If your instance variable specifies null or doesn't specify any value, no object is created but the reference is initialized to null. I don't know if you meant "created" to be optional in that statement.

private String name; // initialized to null, nothing created

And I might ask what "unique" meant in there too. Don't be discouraged by my nit picking. The way you restated the "answer so far" was very helpful, a great way to use the ranch. Keep asking, keep having fun with Java!
 
John Mateer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In reference to instance variables: Instance variable object references and/or primitives are �unique� to an object instantiated in the heap.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps some of the original confusion is due to the fact that the word "instance" is used in different contexts. The above discussion has covered what we mean by "instance variables." When we say an "instance of a class", this is the same as an object. We use references to access these objects (or instances) as discussed above.

Layne
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic