Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to access variables in static ini blocks

 
amal shah
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider the code below:

static
{
int x=5; //1
}
static int x=10; //2
System.out.println(x); //3
now here at line 3 variable at line 2 will be accessed...how can i access variable at line 1 since this variable is local to the the static initialiser block....help appreciated

thanking you
amal shah
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That variable is in scope only inside the block.
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


or so




2. There is no way to get local "x" variable from line 1 after the end of this static init block. Because this variable is LOCAL.
[ August 11, 2006: Message edited by: Ivan Rebrov ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic