首页  >  文章  >  Java  >  Java newInstance()

Java newInstance()

WBOY
WBOY原创
2024-08-30 15:35:12674浏览

每当类需要动态创建新实例时,类的 Java newinstance() 方法就会出现。该方法将在现有方法之上调用,该方法是用于动态加载任何类的 .class 名称。除了该类之外,还将调用此 newInstance() 方法来动态创建对象。该类的 newInstance() 方法不考虑来自该类的任何参数或实参,这意味着它不具有任何构造函数;因此,它可以被称为一个类的无参数构造函数。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

public Tsk newInstance() throws InitiationException, IllegalAccessException

语法流程由以下参数组成,分别代表:

  • 公共:正在定义和声明的类的访问修饰符。
  • Tsk:声明的类的名称,并且是通用的,这意味着它可以像对象类和泛型一样思考。
  • newInstance():除了类的 .class 名称之外,动态创建和加载对象所调用的方法的名称。
  • throws: 用于捕获和抛出异常的关键字。
  • InitiationException: 后跟 throws 关键字,用于捕获最初启动的异常。
  • IllegalAccessException: 用于捕获和访问所有非法或未使用的异常。

newInstance() 方法在 Java 中如何工作?

类的任何 newInstance() 或构造函数都会被调用并用于创建类的新实例。总是首选使用构造函数的 newInstance() 方法,而不是使用类的 newInstance() 方法,因为构造函数的 newInstance() 方法可以使用任意数量的参数,而 newInstance( 则不同) ) 类的方法,因为类的 newInstance() 方法不具有任何参数,这意味着类中的无参数构造函数。另外,有时它会与任何类别的新运算符进行比较和混合。

newInstance()方法的工作流程是这样的,使用new操作符创建对象,然后决定是否需要在运行时或编译时创建对象,或者是否需要创建对象。非常需要动态创建对象。如果决定需要在运行时创建该对象,那么新Operator的创建将是模糊的。因此,要在运行时创建对象并动态加载对象,需要创建并使用 newInstance() 方法。

正如所讨论的,类的 newInstance() 方法将首先创建类类型的对象,然后使用 .class 名称调用来创建新实例。 Class.forName() 方法将返回该类的一个对象,该对象将返回作为参数传递的该类的对象,如果传递参数的类不存在,则会抛出 ClassNotFoundException 异常。 🎜>

当该方法内部调用该类的默认构造函数时,将调用并使用随后抛出的实例化异常。如果无法访问已定义和指定的类,则会发生 IllegalAccessException。因此,推荐使用该类的 newInstance() 方法,因为它为使用动态加载创建对象的 newInstance() 提供了灵活性和多功能性。构造函数和对象的典型行为和调用是不同的,并且有所增强,因为它不包含任何要传递给方法和对象的参数。

Java newInstance() 示例

以下是下面提到的示例:

示例#1

此程序用于说明 Class.forName 方法和 newInstance 方法用于创建对象,然后打印该类的对象以打印动物的值并为异常创建它。

代码:

class Animal {  int p; }
class Birds {  int q; }
public class NewInstanceTst
{
public static void sounds(String s)  throws InstantiationException,
IllegalAccessException, ClassNotFoundException
{
Object obj_1 = Class.forName(s).newInstance();
System.out.println("Creation of newly instantiated class:"
+ obj_1.getClass().getName());
}
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException
{
sounds("Animal");
}
}

输出:

Java newInstance()

示例#2

该程序演示了带有传递参数或构造函数的Java newInstance 类。然后它用于对象的动态分配,但不使用,因为它会抛出非法异常。编写并执行测试类,以验证实例化的类是否可以处理对象的动态加载。

代码:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class NwInstncWithconstructors {
public static void main(String[] args)
throws InstantiationException,
IllegalAccessException,
IllegalArgumentException,
InvocationTargetException
{
Constructor[] constructor_a  = Instnace_Constructor_Test.class.getConstructors();
Instnace_Constructor_Test animal = (Instnace_Constructor_Test)constructor_a[0].newInstance();
System.out.println(animal.content);
}
}
class Instnace_Constructor_Test {
String content;
public Instnace_Constructor_Test()
{
System.out.println("Create a new Instance:");
content = "new_instance_of_the_value";
}
}

Output:

Java newInstance()

Example #3

This program also demonstrates the newInstance class of Java. Still, without passing parameters or constructors, it is used for the dynamic allocation of objects seamlessly and makes the overall class flexible for allocation. Still, if not used, it will throw illegal exceptions. A test class is written and executed to verify whether the instantiated class can handle the object’s dynamic loading. This program calls for a no-arg method which means newInstance class directly.

Code:

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class NewInstanceWithout_Constructors {
public static void main(String[] args)
throws InstantiationException,
IllegalAccessException,
IllegalArgumentException,
InvocationTargetException
{
Constructor[] constructor_without_arr = Without_constructor_Test.class.getConstructors();
Without_constructor_Test sm_ob
= (Without_constructor_Test)constructor_without_arr[0]
.newInstance("one_field_value");
System.out.println(sm_ob.getthat_value());
}
}
class Without_constructor_Test {
private String that_value;
public Without_constructor_Test(String that_value)
{
this.that_value = that_value;
}
public String getthat_value()
{
return that_value;
}
public void setthat_value(String that_value)
{
this.that_value = that_value;
}
}

Output:

Java newInstance()

Note: It is always recommended to use a newInstance class instead of using a newInstance constructor because it can easily perform dynamic loading of the class and is flexible, unlike the newInstance constructor, which is not at all preferable if used with multiple parameters that too at the run time.

Conclusion

newInstance() method of java class is an added advantage as it is used to dynamically load the object without passing multiple parameters, and then it can be used with versatility within the class, and no external method is called at the run time with the help of .classForName method of the class the work gets solved relentlessly.

以上是Java newInstance()的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn