Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
class A
        {
       static {
            System.out.println(y);
          }
       static int y=10;
    }

the above code produce error.
But the below code does not produce error why

Java
class A
{
      static int i=m1();
      static int m1()
       {
            System.out.println(y);
                 return 10;
       }
      static int y=10;
}
Posted
Updated 8-Mar-15 16:21pm
v2

1 solution

See http://www.jusfortechies.com/java/core-java/static-blocks.php[^]. Something you could easily have found for yourself.
 
Share this answer
 
Comments
ajaiswal595 9-Mar-15 5:45am    
The above link doesn't meet my requirement...
Richard MacCutchan 9-Mar-15 5:58am    
Then you need to read it more closely. Static initializers are executed before anything else when the class is first constructed.
ajaiswal595 9-Mar-15 12:24pm    
hello Richard actually both cases will initialize before class construction...
I need some more internal concept.

if you put in static block System.out.println(A.y); then also it will not produce any error. what actually happens internally....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900