Block-Methoden automatisch aufgerufen werden, wenn wir die Klasse in JVM geladen werden, werden zuerst die Blöcke werden ausgeführt, dann werden Haupt Methode auszuführen. Wie
public class ConstructorBlockExample{
{
System.out.println("This is first constructor block");
}
public ConstructorBlockExample(){
System.out.println("This is no parameter constructor");
}
public ConstructorBlockExample(String param1){
System.out.println("This is single parameter constructor");
}
public ConstructorBlockExample(String param1, String param2){
System.out.println("This is two parameters constructor");
}
{
System.out.println("This is second constructor block");
}
public static void main(String[] args){
ConstructorBlockExample constrBlockEx =
new ConstructorBlockExample();
ConstructorBlockExample constrBlockEx1 =
new ConstructorBlockExample("param1");
ConstructorBlockExample constrBlockEx2 =
new ConstructorBlockExample("param1", "param2");
}
}
und Output ist
This is first constructor block
This is second constructor block
This is no parameter constructor
This is first constructor block
This is second constructor block
This is single parameter constructor
This is first constructor block
This is second constructor block
This is two parameters constructor
Und Sie können für einen Block verwenden Etikett, aber es meist verwendet zu brechen oder die Schleife in Block wie weiter:
outer:
for(int i=...){
for(int j=..){
...
break outer; // leaves both loops
}
}
Warum nach unten ... Könnte jemand aussortieren erklären mir n n mir helfen, das Konzept statt downwarding zu verstehen. M ein Neuling in Java. – ZUKA
Es ist nicht lösbar. Und ja, du gehst in die falsche Richtung. –
@Thomas Aber mit break und continue erreichen wir diesen Block – ZUKA