This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Quick question about instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quick question about instance variables" Watch "Quick question about instance variables" New topic
Author

Quick question about instance variables

Jake Miller
Ranch Hand

Joined: Jun 27, 2007
Posts: 43
Here is a class:


And an object:


So what exactly are the instance variables in this situation? Size and name? Name is not used again, so I am not even entirely sure why it is used.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

not sure why name it there either if it is not used, yes size and name are instance variables because you can call them using one.size, two.size. If size or name was static then they would not be instance variables but merely variables. If I am wrong someone correct me please.


Visit my blog! http://jameshambrick.com
Jon Anslow
Greenhorn

Joined: May 05, 2006
Posts: 16

Originally posted by James Hambrick:
if size or name was static then they would not be instance variables but merely variables.


Just to confirm, if size and name were static then they would be known as Class variables and would be accessed via the class name. A reference of the Dog Class would not need to be created to use them.

eg:
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
In class Dog, size and name are instance variables.

In class DogTestDrive, one, two, and three are local variables on the stack.

I've seen this example before and I think it is part of a series of "snippets" and that later a constructor is used to pass in the name when a dog is instantiated.


------------------------
Bob
SCJP - 86% - June 11, 2009
 
Consider Paul's rocket mass heater.
 
subject: Quick question about instance variables
 
Similar Threads
Static nad non static Access
If statement now exectutes both if staments but didn't used to
Head First GoodDog code error
Two classes defined in the same source file ??
return values in methods