Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does this code work? ("static" keyword)

Status
Not open for further replies.

aplusc

Programmer
Feb 17, 2002
44
US
I came across this code with a very bizzare syntax, but apparently it compiles and runs without an error:

class z{
z(String s){
System.out.println(s);
System.exit(0);
}
static{new z("Hello, World!");}
}

I understand everything that happens in the constructor, but what's going on with

static{new z("Hello, World!");}

...? Is this some kind of a shot-hand syntax for public static void main() ?
 
>> Is this some kind of a shot-hand syntax for public
>> static void main()

Well no since you have no access to the process command line. It's just a static code block which by definition is executed at initialization as are all things static.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top