「java11」getDeclaredConstructor().newInstance()でクラスのインスタンス対象を生成するサンプル

書式
クラス名.class.getDeclaredConstructor().newInstance();

使用例
1.Testクラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.info;
//クラスの定義
public class Test {
public int sn = 20;
public static void main(String[] args) {
// クラスのオブジェクトを生成
Test obj = new Test();
// 自分のメソッドを呼び出す
String res = obj.funcAA();
System.out.println(res);
}
public String funcAA() {
return "study skill in arkgame";
}
}
package com.arkgame.info; //クラスの定義 public class Test { public int sn = 20; public static void main(String[] args) { // クラスのオブジェクトを生成 Test obj = new Test(); // 自分のメソッドを呼び出す String res = obj.funcAA(); System.out.println(res); } public String funcAA() { return "study skill in arkgame"; } }
package com.arkgame.info;

//クラスの定義
public class Test {

      public int sn = 20;

      public static void main(String[] args) {
            // クラスのオブジェクトを生成
            Test obj = new Test();
            // 自分のメソッドを呼び出す
            String res = obj.funcAA();
            System.out.println(res);
      }

      public String funcAA() {

            return "study skill in arkgame";
      }

}

2.クラスのインスタンスを生成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.info;
import java.lang.reflect.InvocationTargetException;
public class InstanceDmo {
public static void main(String[] args) {
try {
Test obj = Test.class.getDeclaredConstructor().newInstance();
System.out.println("メンバーを呼ぶ:" + obj.sn);
System.out.println("関数を呼ぶ:" + obj.funcAA());
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
package com.arkgame.info; import java.lang.reflect.InvocationTargetException; public class InstanceDmo { public static void main(String[] args) { try { Test obj = Test.class.getDeclaredConstructor().newInstance(); System.out.println("メンバーを呼ぶ:" + obj.sn); System.out.println("関数を呼ぶ:" + obj.funcAA()); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }
package com.arkgame.info;

import java.lang.reflect.InvocationTargetException;

public class InstanceDmo {

      public static void main(String[] args) {

            try {
                  Test obj = Test.class.getDeclaredConstructor().newInstance();
                  System.out.println("メンバーを呼ぶ:" + obj.sn);
                  System.out.println("関数を呼ぶ:" + obj.funcAA());
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
                        | NoSuchMethodException | SecurityException e) {
                  // TODO 自動生成された catch ブロック
                  e.printStackTrace();
            }
      }

}

結果
メンバーを呼ぶ:20
関数を呼ぶ:study skill in arkgame

Java

Posted by arkgame