What is the difference between static variable and instance variable?

Showing Answers 1 - 7 of 7 Answers

subba reddy

  • Nov 22nd, 2007
 

Static variable the field is allocated when the class is created. It belongs to the class and not any object of the class. It is class variable

Instace variable the field is allocated when the class is instanciated to the class is called instance variable or non-static variable

lefty

  • Jan 16th, 2008
 

The answer given is absolutely right along with that we also say the following

1. Static variable has only one copy for all the methods in class while instance variable has
 many copy.
2. Class can access only static variable while object can access both class and instance
 variable.  

pavsgreat

  • Jun 29th, 2010
 

A static variable is shared among all instances of a class, where a non-static variable - also called an instance variable - is specific to a single instance of that class.

  Was this answer useful?  Yes

pksudha03

  • Jun 30th, 2010
 

Static Variables are treated common for the whole class. We can say that static variables are class specific, instructing the compiler that only one copy of the variable is there and all other objects should use the same variable. Instance variables are instance specific. For eg, there is a class named "Box" and we have 2 objects b1,b2.

class Box
{
static int A;
int b,c;
}
Class Boxdemo
{
Public static void main(Sting args[])
{
Box b1=new Box();
Box b2=new Box();

Box.A=1;
}
}

From this example, we are clear that  static variable A is common for the whole class and there will be the copy of instance varibls b and c for object b1 and there will be the seperate copy of the instance variable b and c for the object b2;

  Was this answer useful?  Yes

Static variables are class variables.  These variables can be called by using the class.variables name. These can be accessed withing the class and outside the class.  These variables not require any instance of the class.

Instance variables are member variables. To call these variables we need to use super() keyword. These variables require instace of the class.

  Was this answer useful?  Yes

Isaaq

  • Mar 13th, 2013
 

1) An instance variable is one whose memory space is creating each and every time whenever an object is created.
1) Static variables are whose memory space is creating only once when the class is loaded by class loader subsystem (a part of JVM) in the main memory irrespective of number of objects.
2) Programmatically instance variable declaration should not be preceded by keyword static.
2) Programmatically static variable declaration must be preceded by keyword static.
3) Data type v1, v2…vn;
3) Static data type v1, v2…vn;
4) Instance variable must be accessed with respect to object name i.e., objname.varname;
4) Static variables must be accessed with respect to class name i.e., classname.varname;
5) Value of instance variable is not sharable.
5) Value of static variable is always recommended for sharable.
6) Instance variable are also known as object level data members since they are dependent on objects.
6) Static variable are also known as class level data members since they are dependent on classes.

  Was this answer useful?  Yes

sravani

  • Jun 3rd, 2013
 

Those are variables static can be defined where the variable is constant,instance variable can be defined instance of class object

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions