0

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 by mith_cool: n/a

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

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 by Majestics: n/a

0

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 by JamesCherrill: n/a

0

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

0

.. 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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.