aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static initialization" Watch "Static initialization" New topic
Author

Static initialization

Andry Dub
Ranch Hand

Joined: Nov 06, 2007
Posts: 43
Hi! Why i can't access z from method goB2 of B2 class?

public class Broom {
static private int x=8;

static{
int z=2;
}

static class B2{
static int y=9;
void goB2(){
System.out.println(x+" "+y+" "+z);
}
}
}


Java is my love
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41532
    
  53
z only exists in the block in which it is declared, in this case the static initializer. As soon as that is exited, z ceases to exist. Maybe you meant to write the following:



Ping & DNS - my free Android networking tools app
Andry Dub
Ranch Hand

Joined: Nov 06, 2007
Posts: 43
Thank you. Now it is clear for me
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static initialization