*
The moose likes Beginning Java and the fly likes Instance Vs Class 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 "Instance Vs Class variables" Watch "Instance Vs Class variables" New topic
Author

Instance Vs Class variables

ppavya india
Greenhorn

Joined: Sep 27, 2004
Posts: 19
Hi all,
I am new to Java tech., please explain me the difference between Instance variables and Class variables with appropriate example.

Thanx in advance
danesh far
Greenhorn

Joined: Nov 25, 2004
Posts: 15
Hi,
We use a class to creat different objects.These objects have some common aspects which are same in all of the objects.For example the minimum amount of money for opening an account is same for all of the objects.This is an class varible but objects balance is a instance variable since it is different for different object so it is an instance variable.You can declear a class variable as a instance variable but when you want to change it you have to change it in all of objects.
ppavya india
Greenhorn

Joined: Sep 27, 2004
Posts: 19
Hi Danesh,
Could u please do one thing, define a class file and tell me which are the class variables and which are the instance variables. Waiting for ur reply.

Thanx
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In Java the "static" keyword makes something a class variable. You'll hear static and class used interchangeably. When the JVM loads a class it reads a lot of information about the class like what methods it has and what variables and so on. It puts all this information in an object that you normally don't see unless you get into reflection. Static/Class variables go on this object. So for a given class there is exactly one copy of the static variable.

When you create instances of an object the JVM allocates new copies of any variables that are not static. These variables belong to the object instance, so each instance can have its own value.

To help keep things straight, you might adopt a style of always referencing static variables by the class name and instance variables by the object name. The Eclipse IDE has an option to make it a warning when you reference a static variable by an instance.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
danesh far
Greenhorn

Joined: Nov 25, 2004
Posts: 15
An instance data value is used to maintain information specific to individual instances. For example, each Account object maintains its balance.
A class data value is used to maintain information shared by all instances or aggregate information about the instances.

For example, minimum balance is the information shared by all Account objects, whereas the average balance of all Account objects is aggregate information.
An example of an instance data value:
Three Account objects possess the same data value current balance, but the actual dollar amounts differ.

Classs Test {
private static int minimu_balance=100; //class data
private int current_Balance; //instance data
.
.
.
}


}
ppavya india
Greenhorn

Joined: Sep 27, 2004
Posts: 19
Thanx, thanx a lot.
 
 
subject: Instance Vs Class variables
 
Similar Threads
Spot the difference
Garbase Collection
Pl explain this a bit diff code
Variables in Java
static