• 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

Instance variables and primitive variables

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'm just a little bit confused about instance variables and primitive variables. When we say instance variables, they are the variables which are not inside a method or shall we say "local variables". Does it mean also that those primitive variables outside a method are instance variables? such as: int i;, long l = 20L; and so on.. and primitive variables inside a method are local variables?
Or instance variables are reference of a class and object but not primitive data declared either outside or inside of a method.

Thank you guys!

regards,
Denver
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Denver,
I suggest you to read a good book on Java starting with basics like Kathy Sierra. It will really help and clear all of your confusion.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variables which are not in any method and in the class are all instance variable except those explicitly declared static .....
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Denver,
It is not necessarily true that a variable outside any method is an instance variable. This is because it could be a class (denoted by static) variable. Static or class variables do not belong to the instances but they belong to the whole class. An example of static variable would be a static integer that keeps a count of how many instances or objects you created in the program.
Hope it helped you understand the point better.

Vishal
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not compare "primitive variables" with "instance variables". If you want to do a differentiation on variables, better to compare the following two pairs, it will give you a clear picture;
- primitive variables / reference variables
- class variables / instance variables

primitive variables are of type byte, short, int, long, float, double, char, boolean. Reference variables are pointing to objects in the heap, therefore the primitive variables are excluded since those are not considered as objects in java.

When comparing class and instance variables, the "static" keyword comes in to play. The variables which are marked as static are class variables and non-static variables are considered as instance variables. For a class variable there is only one copy of the variable available for all the objects(instances) of that class. So if you make any changes to the class variable all the objects of that type gets affected since all are pointing to the same memory location in the heap.
(NOTE : class variables can be accessed through both class name or the instance names, but it is recommended to access through the class name.)

When considering the instance variables, each object of the class are having a separate copy of the variable. Therefore when you do a change to an instance variable through the instance name, only the copy which belongs to that particular instance is modified and the values of other objects remains the same. (NOTE : instance variable can be accessed only through instance names.)

Variables inside methods are called "local variables" and belongs only to the method scope.

Let me know if any further explanation is needed.
If you are planning to do the SCJP, i recommend you to read Kathy Sierra's book. It will clear all your doubts.
 
reply
    Bookmark Topic Watch Topic
  • New Topic