• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static initialization

 
Andry Dub
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Andry Dub
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Now it is clear for me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic