• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Use of static keyword

 
Greenhorn
Posts: 6
  • 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
 
Ranch Hand
Posts: 44
  • 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
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic