-1

IN java Language ,when we create an object in method, is Static variables are load in the object ?

public class A{     
      static int x =12; 

       void m1(){ 
               int x=2;
              A a = new A(); 
               a.x=3;
           System.out.println(a.x); 

       } 
    public static void main(String[] args) { 
        A a = new A(); 
          a.m(); 


    } 
} 
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
Paradox
  • 31
  • 3

3 Answers3

0

is Static variables are load in the object ?

No,they are not.Static variables are class level variables.

See Also

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
0

Static variables are only created once, at the first time an instance is created. So creating a second instance will not change the value of x. For this example the output should be 3.

Gi0rgi0s
  • 1,757
  • 2
  • 22
  • 31
0

Static variable’s value is same for all the object(or instances) of the class or in other words you can say that all instances(objects) of the same class share a single copy of static variables.

They are loaded at runtime.

Static means: that the variable belong to the class, and not to instances of the class. So there is only one value of each static variable, and not n values if you have n instances of the class.

This is the code free of mistakes.

 public class A{     
  static int x =12; 

   void m1(){ 
           x=2;
          A a = new A(); 
           a.x=3;
       System.out.println(a.x); 

   } 
public static void main(String[] args) { 
    A a = new A(); 
      a.m1(); 


  } 
} 
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • sorry guys I have some mistake There should be a.m1(); in the main method so any way if the static variables are not load in the object why the value in X is change in my Question...??? – Paradox Dec 02 '15 at 11:19