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

Recommended Answers

All 4 Replies

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.