aspose file tools*
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
 
 
subject: Use of static keyword