Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package package1;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- class Student {
- int a;
- int b;
- // public Student(int a, int b){
- // this.a = a;
- // this.b = b;
- // }
- // public int getA(){
- // return a;
- // }
- //
- // public int getB(){
- // return b;
- // }
- public Student(){
- a= 5;
- b= 10;
- }
- public void print(){
- System.out.println(a+b);
- }
- }
- public class MainClass {
- public static void main(String[] args) {
- Student st;
- try {
- st = Student.class.newInstance();
- st.print();
- Constructor <Student> constructor = Student.class.getConstructor();
- Student student = constructor.newInstance();
- student.print();
- Class cls = Class.forName("package1.Student");
- Constructor con = cls.getConstructor();
- Object obj = con.newInstance();
- // Class cl = Class.forName("Student");
- // Class type[] = {int.class, int.class};
- // Constructor cons = cl.getConstructor(type);
- // Object newInstanceObject = cons.newInstance(22, 35);
- // Student st = (Student) newInstanceObject;
- // st.print();
- // Constructor c = Class.forName("Student").getConstructor(new Class[]{int.class, int.class});
- // Student st2 = (Student)c.newInstance(45, 55);
- // System.out.println(st2.getA());
- Method methods[] = cls.getDeclaredMethods();
- for (Method method : methods) {
- method.invoke(obj);
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement