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

Return to Question

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)?

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)?

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)?

Dup relationship reversed. Removing banner (see other question to see dup), minor other clean ups.
Source Link

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)?

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)

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)?

Post Reopened by Shog9
title
Source Link
user166390
user166390

Why is this Java code in curly braces, ({}) outside of a method?

Why is this Java code in curly braces, ({}) outside of 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)

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)

Why is this Java code in curly braces ({}) outside of 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)

insert duplicate link
Source Link
Loading
Source Link
nairdaen
  • 1k
  • 2
  • 11
  • 19
Loading
lang-java

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