在Java使用实例块
实例块可以定义为Java中的无名方法,我们可以在其中定义逻辑,它们具有以下一些特性。它们可以在类中声明,但不能在任何方法中声明。实例块逻辑对所有对象都是通用的。在创建过程中,每个对象的实例块只会执行一次。
插图:
class GFG {
{
// Code inside instance block
}
}
Instance块的优点如下:
每当创建任何类型的对象时都会执行实例块。如果我们想编写一个我们想要在创建各种对象时执行的逻辑,那么使用实例块是一个好主意,以避免在每个构造函数中编写相同的逻辑。
Instance 块的缺点如下:
我们一般不使用它们来初始化对象,因为它们不能接受参数。如果我们仍然使用实例块进行初始化,那么所有对象都必须使用相同的值进行初始化,这实际上是无用的。
示例 1:
Java
// Java Program to Illustrate Usage of Instance Blocks
// Class 1
// Helper class
class GFG {
// Constructors of this class
// Constructor 1
// This constructor will get executed for 1st
// kind of object
GFG()
{
System.out.println("1st argument constructor");
}
// Constructor 2
// This constructor will get executed for
// 2nd kind of object
GFG(String a)
{
// Print statement when this constructor is called
System.out.println("2nd argument constructor");
}
// Constructor 3
// This constructor will get executed
// for 3rd kind of object
GFG(int a, int b)
{
// Print statement when this constructor is called
System.out.println("3rd arguments constructor");
}
{
// Creation of an instance block
System.out.println("Instance block");
}
}
// Class 2
// Main class
class GFGJava {
// main driver method
public static void main(String[] args)
{
// Object of 1st kind
new GFG();
// Object of 2nd kind
new GFG("I like Java");
// Object of 3rd kind
new GFG(10, 20);
}
}
Java
// Java Program to Illustrate Execution of Instance Blocks
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Making object of class in main()
GFG geek = new GFG();
}
// Constructor of this class
GFG()
{
// Print statement when constructor is called
System.out.println("I am Constructor!");
}
{
// Print statement when instance block is called
System.out.println("I am Instance block!");
}
static
{
// Print statement when static block is called
System.out.println("I am Static block!");
}
}
输出
Instance block
1st argument constructor
Instance block
2nd argument constructor
Instance block
3rd arguments constructor
Note: The sequence of execution of instance blocks follows the order- Static block, Instance block, and Constructor.\\
从下面提出的例子可以证明它是合理的:
示例 2:
Java
// Java Program to Illustrate Execution of Instance Blocks
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Making object of class in main()
GFG geek = new GFG();
}
// Constructor of this class
GFG()
{
// Print statement when constructor is called
System.out.println("I am Constructor!");
}
{
// Print statement when instance block is called
System.out.println("I am Instance block!");
}
static
{
// Print statement when static block is called
System.out.println("I am Static block!");
}
}
输出
I am Static block!
I am Instance block!
I am Constructor!