Unterschiede und Codebeispiele für gewöhnliche Codeblöcke, konstruierte Codeblöcke, statische Codeblöcke in Java
// Ausführungsreihenfolge: (Priorität von hoch nach niedrig.) Statische Codeblock> Mian -Methode> CODE -Block konstruieren> Konstrukt -Methode.
Der statische Codeblock wird nur einmal ausgeführt. Der konstruierte Codeblock wird jedes Mal ausgeführt, wenn das Objekt erstellt wird.
1 Normaler Codeblock
// gewöhnlicher Codeblock: {}, der in einer Methode oder Anweisung angezeigt wird, wird als normaler Codeblock bezeichnet. Die Ausführungsreihenfolge der gewöhnlichen Codeblöcke und allgemeinen Anweisungen wird durch die Reihenfolge festgelegt, in der sie im Code erscheinen - "Erste" Erste "öffentliche KlassencodeBlock01 {public static void main (String [] args) {{int x = 3; System.out.println ("1, Variable x ="+x); } int x = 1; System.out.println ("Variable x ="+x); {int y = 7; System.out.println ("2, variable y ="+y); }}} / * Auslaufergebnis: 1, Variable x = 3 in normaler Codeblockvariable x = 1 2, variable y = 7 in normaler Codeblockvariable y = 7 * / / / / /2 Codeblöcke konstruieren
// Konstruktorblock: Ein Codeblock, der direkt in der Klasse definiert ist und kein statisches Schlüsselwort hat, wird als {} -Konstruktionscodeblock bezeichnet. Der Konstruktorcodeblock wird beim Erstellen eines Objekts aufgerufen, und jedes Mal, wenn das Objekt erstellt wird, wird er aufgerufen, und die Ausführungsreihenfolge des Konstruktorcodeblocks hat Vorrang vor dem Klassenkonstruktor. public class codeBlock02 {{system.out.println ("erster Code -Block"); } public codeBlock02 () {System.out.println ("Konstruktionsmethode"); } {System.out.println ("Second Constructor Block"); } public static void main (String [] args) {new codeBlock02 (); neuer CodeBlock02 (); neuer CodeBlock02 (); }} /** Ausführung Ergebnis: Erster Code Block Zweiter Konstruktor -Block -Konstruktionsmethode Erster Code Block Zweiter Konstruktorblock -Konstruktionsmethode Erster Code Block Zweiter Konstruktor -Block -Block -Konstruktionsmethode* /3 Statische Codeblöcke
// Statischer Codeblock: Codeblock mit statischer Schlüsselwort in Java deklariert. Statische Blöcke werden verwendet, um Klassen zu initialisieren und die Attribute der Klasse zu initialisieren. Jeder statische Codeblock wird nur einmal ausgeführt. Da das JVM beim Laden der Klasse statische Codeblöcke ausführt, werden die statischen Codeblöcke vor der Hauptmethode ausgeführt. // Wenn die Klasse mehrere statische Codeblöcke enthält, folgt sie von "zuerst definierter Code und später definierter Code". // HINWEIS: 1 Statische Codeblöcke können in keiner Methodekörper vorhanden sein. 2 Statische Codeblöcke können nicht direkt auf statische Instanzvariablen und Instanzmethoden zugreifen und über das Instanzobjekt der Klasse zugegriffen werden müssen. Klassencode {{System.out.println ("Konstruktor des Code"); } static {System.out.println ("Konstruktor des Code"); } public code () {System.out.println ("Konstruktor des Code"); }} public class codeBlock03 {{system.out.println ("Konstruktor von CodeBlock03"); } static {system.out.println ("Konstruktor von CodeBlock03"); } public codeBlock03 () {System.out.println ("Konstruktormethode von CodeBlock03"); } public static void main (String [] args) {System.out.println ("Hauptmethode von codeblock03"); neuer Code (); neuer Code (); neuer CodeBlock03 (); neuer CodeBlock03 (); }}/ *CodeBlock03 statischer Code Block CodeBlock03 Hauptmethode Code statischer Code -Block -Code -Konstruktor -Code -Konstruktor -Konstruktor -Code -Konstruktor -Code -Konstruktor -Konstruktor -Code -Konstruktor -Konstruktor -Konstruktor -Konstruktor -Code -Konstruktor -Konstruktor -Konstruktor -Code -Code -Blockcode -Block -Code -Block -Codeblock03 -Konstruktionsmethode Codeblock03 Construction -Methode constructor constructor codeblock03 construktionsmethodeDanke fürs Lesen, ich hoffe, es kann Ihnen helfen. Vielen Dank für Ihre Unterstützung für diese Seite!