Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

2 of 5
insert duplicate link

Java code in curly braces, outside a method

Possible Duplicate:
Is this valid Java code? My teacher claims it is, but I'm really not so sure.

Hello,

I am getting ready for a java certification exam and I have seen code LIKE this in one of the practice tests:

class Foo { 
int x = 1; 
public static void main(String [] args) { 
 int x = 2; 
 Foo f = new Foo(); 
 f.whatever(); 
} 
{ x += x; } // <-- what's up with this?
void whatever() { 
 ++x; 
 System.out.println(x); 
} 
}

My question is ... Is it valid to write code in curly braces outside a method? What are the effects of these (if any)

nairdaen
  • 1k
  • 2
  • 11
  • 19
lang-java

AltStyle によって変換されたページ (->オリジナル) /