| Author |
a question about static clause
|
Steven Gao Song
Ranch Hand
Joined: Oct 02, 2006
Posts: 78
|
|
public class Static { static { int x = 5; } static int x, y; public static void main(String[] args) { x--; myMethod(); System.out.println(x + y + ++x); } public static void myMethod() { y = x++ + ++x; } } I cannot understand why "int x = 5;" is not executed. Thank you.
|
 |
Saira Dhanani
Greenhorn
Joined: Sep 07, 2006
Posts: 3
|
|
I cannot understand why "int x = 5;" is not executed. The Static block is executed i.e. int x = 5 is executed but int x declared in the static block is local to the block hence it has no effect on / is not related to the static variable x.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
The static block is executed and a local int variable is set to 5.
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Steven Gao Song
Ranch Hand
Joined: Oct 02, 2006
Posts: 78
|
|
|
I got it. Thanks a lot.
|
 |
 |
|
|
subject: a question about static clause
|
|
|