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("package1.Lmao");
- // Class type[] = {};
- Constructor cons = cl.getConstructor(Integer.TYPE, String.class);
- Object obj = cons.newInstance(78, "Siddharth");
- Student st = (Student) obj;
- st.print();
- // Constructor c = Class.forName("Student").getConstructor(new Class[]{int.class, int.class});
- // Student st2 = (Student)c.newInstance(45, 55);
- // st2.print();
- // 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