This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of static keyword 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 "Use of static keyword" Watch "Use of static keyword" New topic
Author

Use of static keyword

Neeraj Kumar
Greenhorn

Joined: Apr 10, 2003
Posts: 6
In the following piece of code, "static",a keyword, is used like a function. Yet the compilation succeds and it executes as well.
public class Static
{
static
{
int x=5 ;
}
static public int x,y ;
public static void main(String[] args)
{
x-- ;
myMethod() ;
System.out.println(x+y+ ++x);
}
public static void myMethod()
{
System.out.println(y = x++ + ++x) ;
}
}
However, use of any keyword as identifiers is prohibited.
Can any one explain it why
pinky yadav
Ranch Hand

Joined: Jun 17, 2002
Posts: 44
Hi,
In the code supplied the static is not the method name. That block of code is a static initializer.
From JLS
Any static initializers declared in a class are executed when the class is initialized and, together with any field initializers (�8.3.2) for class variables, may be used to initialize the class variables of the class.
StaticInitializer:
static Block

Pinky
 
Consider Paul's rocket mass heater.
 
subject: Use of static keyword
 
It's not a secret anymore!