• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Quick question about instance variables

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 16
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!