Link to home
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

diff between static and instace varaibles

Is instance variables and member variables one and the same;

i am little bit confusion abt static varaibles and instance varaibles

in the case of static varaibles only one instance will be created for that particualr class(can anybody show me one good example);In case of instance varaibles for every object one instance  will be created.

we can call the methods will the class name(static)
Myclass.ex();

we can call the methods will the object(instance)
Myclass e = new Myclass();
e.ex();'
Avatar of sudhakar_koundinya
sudhakar_koundinya

class A
{
   public  int a=10; //member variable
    public static int b=15;
    public static void main(String s[])
   {
               A x= new A();
              x.a=15;//instance  variable;
           A.b=20;//static variable
          x.b=20;//it is also valid, although
   }
}
SOLUTION
Avatar of JakobA
JakobA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
> Is instance variables and member variables one and the same

Not exactly.
"Class variable" is any static variable. Static variable will be created only once, when class is loaded. And there will be only one instance of this variable for all the objects of the class.
"Instance variable" is any variable related to object instance. Such variable will be created when new object is created. And each object will have its own instance of this variable.
"Member variable" is any variable (both "class" and "instance") being part of the class. You use this term in contrast to "local variable", that you create inside methods.
SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
I can't quite believe what I just did! I actually **did not** read JakobA's allusion to houses and their rooms!!

Sorry JakobA - it as a one in a million coincidence.

I retract my analogy, and defer to JakobA.
;)
> Is instance variables and member variables one and the same;

they are the same, just different names