Instance variable does mean you will get new value with every new object created of particular class rite? I tried it myself
eg:

class simpleinstcheck
{
	static int i;
	void incr()
	{
		System.out.println(i++);
	}
	public static void main(String args[])
	{
		simpleinstcheck sc= new simpleinstcheck();
		sc.incr();
		simpleinstcheck sc1= new simpleinstcheck();
		sc1.incr();
		simpleinstcheck sc2= new simpleinstcheck();
		sc2.incr();
	}
}

So the output is now 0,1,2.... So i got to ask u if its an instance variable is it denoted with static keyword??
coz wen i tried the same thing without staic keyword i got output:- 0,0,0

So wer the value of that variable remains fixed irrespective of objects created of that class is known as class variable.... please do correct me coz i googled a lot n m damn confused if m going rite way.... thanks

Edited 5 Years Ago by mith_cool: n/a

Static keyword is visible to all instance so its updated from 0 to 2, while when u dont use static then "i" original value get printed, which is new for each instance.

Edited 5 Years Ago by Majestics: n/a

You are OK about instance & class vars, instance variable are one per object created, class vars are one shared between all objects of that class. but the static keyword defines a class var. It's an instance var if it does NOT have the static keyword.

Edited 5 Years Ago by JamesCherrill: n/a

thank you... It was helpful.. can i see any eg of these variables??

.. can i see any eg of these variables??

Sure, just look in any Java code!
Try enhancing your own little test - have one static one non-static int, increment them both in incr() and see what happens.

This question has already been answered. Start a new discussion instead.