首頁  >  文章  >  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