aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about static clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about static clause" Watch "a question about static clause" New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about static clause