File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of static keyword

 
Neeraj Kumar
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic