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 - 18 of 18 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

  2. package com.practise;

  3.  

  4. public class StaticStringDemo

  5. {

  6.         static String strObject1;

  7.         String strObject2;

  8.  

  9.         public static void main(String args[])

  10.         {

  11.                 StaticStringDemo object1 =new   StaticStringDemo();

  12.                 StaticStringDemo object2 =new   StaticStringDemo();

  13.                 StaticStringDemo object3 =new   StaticStringDemo();

  14.  

  15.                 object1.strObject1="Arvind";

  16.                

  17.                 System.out.println("object1 : "+object1.strObject1);

  18.                 System.out.println("object2 : "+object2.strObject1);

  19.                 System.out.println("object3 : "+object3.strObject1);

  20.                

  21.                 StaticStringDemo at=new StaticStringDemo();

  22.                 at.display();

  23.         }

  24.  

  25.        

  26.         void display()

  27.         {

  28.                 StaticStringDemo object4=new StaticStringDemo();

  29.                 StaticStringDemo object5=new StaticStringDemo();

  30.                 StaticStringDemo object6=new StaticStringDemo();

  31.                

  32.                 object4.strObject2="Siva";

  33.                

  34.                 System.out.println("object 4 : "+object4.strObject2);

  35.                 System.out.println("object 5 : "+object5.strObject2);

  36.                 System.out.println("object 6 : "+object6.strObject2);

  37.  

  38.         }

  39. }

  40.    Output:

  41. object1 : Arvind

  42. object2 : Arvind

  43. object3 : Arvind

  44. object 4 : Siva

  45. object 5 : null

  46. object 6 : null

  47.  

  48.  




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