📜  如何在不使用Java创建对象的情况下执行实例初始化块 (IIB)?

📅  最后修改于: 2022-05-13 01:55:51.811000             🧑  作者: Mango

如何在不使用Java创建对象的情况下执行实例初始化块 (IIB)?

在Java程序中,可以对方法、构造函数和初始化块执行操作。实例初始化块或 IIB 用于初始化实例变量。我们知道,实例块是Java中的无名方法,我们可以在其中定义逻辑,它们具有一定的特性。实例块逻辑对所有对象都是通用的,并且在每个对象创建期间只会执行一次。现在,让我们通过创建对象来看看实例块的正常执行。

演示一:实例块的正常执行

Java
// Executing instance block
// by creating object.
  
// Class 1
// Helper class
class GFG {
  
    {
        // Creation of an instance block
        System.out.println("Instance block called by creating objects");
    }
}
  
// Class 2
// Main class
class GFGJava {
  
    // main driver method
    public static void main(String[] args)
    {
  
        // Object of 1st kind
        GFG obj1 = new GFG();
  
        // Object of 2nd kind
        GFG obj2 = new GFG();
  
        // Object of 3rd kind
        GFG obj3 = new GFG();
    }
}


Java
// Executing instance
// block by creating object.
  
// Class 1
// Helper class
class GFG {
  
    {
        // Creation of an instance block
        System.out.println(
            "Instance block called by creating objects");
    }
}
  
// Class 2
// Main class
class GFGJava {
  
    // main driver method
    public static void main(String[] args)
    {
        // Declaring instance block inside main method
        {
            System.out.println(
                "Instance block inside main method called without creating an object");
        }
  
        // Object of 1st kind
        GFG obj1 = new GFG();
  
        // Object of 2nd kind
        GFG obj2 = new GFG();
  
        // Object of 3rd kind
        GFG obj3 = new GFG();
    }
}


输出:

Instance block called by creating objects
Instance block called by creating objects
Instance block called by creating objects

说明:实例块在每个对象创建时只执行一次。在这里,它为 obj1、obj2 和 obj3 执行。

演示2:执行实例块而不创建对象

在演示 1 中,我们通过创建对象来执行实例块,这很明显,但存在一个误解,即如果不创建不正确的对象就无法执行实例块。在我们的下一个演示中,我们将看到如何在不创建对象的情况下执行实例块。

Java

// Executing instance
// block by creating object.
  
// Class 1
// Helper class
class GFG {
  
    {
        // Creation of an instance block
        System.out.println(
            "Instance block called by creating objects");
    }
}
  
// Class 2
// Main class
class GFGJava {
  
    // main driver method
    public static void main(String[] args)
    {
        // Declaring instance block inside main method
        {
            System.out.println(
                "Instance block inside main method called without creating an object");
        }
  
        // Object of 1st kind
        GFG obj1 = new GFG();
  
        // Object of 2nd kind
        GFG obj2 = new GFG();
  
        // Object of 3rd kind
        GFG obj3 = new GFG();
    }
}

输出:

C:\Users\Bikash\Desktop\GeeksforGeeks Java>javac GFG.java
C:\Users\Bikash\Desktop\GeeksforGeeks Java>java GFG
Instance block inside main method called without creating an object
Instance block called by creating objects
Instance block called by creating objects
Instance block called by creating objects

说明:也可以在不创建对象的情况下执行实例块。要在不创建对象的情况下执行实例块,我们需要在 main 方法中显式定义它。