What is the difference between static and non-static variables

A static variable is associated with the class as a whole rather than with specific instancesof a class. Non-static variables take on unique values with each object instance.

Showing Answers 1 - 6 of 6 Answers

naveenkumarb

  • Jun 23rd, 2007
 

Instance variables can be accessed only by the Instance methods only.

Static variables can be accessed by any method(static or Instance methods)

  Was this answer useful?  Yes

snehlata

  • Sep 24th, 2007
 

Static variables belongs to class only they have no relation to object. They allocate memory when class is loaded at run time. That's why we can call them by the name of class variables.
Non-static variables belongs to object. They allocate memory at compile time.

  Was this answer useful?  Yes

vishal

  • Oct 7th, 2011
 

Static variable:
1)Memory allocated before creation of object.
2)static variables are class variables and the values remains
same fr the whole class and its value is same for all classes in a program.
3) There is only one copy of static variable and even
when the class is instantiated, the value remains the same.
4)Static variables value is automatically show(there is no need to create an object.)

Non Static variable:
1)Every time the class is instantiated, the object has
their own copy of these variables.
2)Non static value is called by creating an object.
3)Non-Static Variables are loading only when an object is
creating for the particular class

  Was this answer useful?  Yes

vinodhini.A

  • Aug 19th, 2012
 

STATIC VARIABLE

1)outclass is called static variable

2)static variable access member of its enclosing class through on object.

NON-STATIC VARIABLE

1)inner class is called non-static variable
2)nested class and it has access to all of the variable and methods object of its earlier.

  Was this answer useful?  Yes

Arvindanathan

  • Dec 3rd, 2013
 

Code
  1. //feel it by doing"Arvind""object1 : ""object2 : ""object3 : ""Siva""object 4 : ""object 5 : ""object 6 : "




Static variables are class variables, we can access these variables using class name and the variable value remains same for all the objects. Generally we can use these static variables as constants for example see below.

static final String str = "geekinterview";

So we can access the above variable using ClassName.str.

Non static variables are also called as instance variables or object variables. The scope of the variables value is until the object remains in JVM. When ever the particular object is removed form JVM then variable value is also lost.

  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