How to call Private Methods Publicly in Java using Reflection API?

java_logo

There is a common question people ask, can we invoke private methods of a class publicly? YES we can call private methods of a class in another class by using Reflection APIjava.lang.reflect package use in reflection API.

There, i am going to write the code in two ways :
1. By using Object's class getClass() method.
package reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class A {
private void test() {
System.out.println("from test");
}

private static void testStatic() {
System.out.println("from static test");
}
}

public class MainAccess {
public static void main(String args[]) {
A obj = new A();
Class c = obj.getClass(); //getClass() method is from Object class
try {
Method m = c.getDeclaredMethod("test"); // from reflection
m.setAccessible(true);
Object o = m.invoke(obj, new Object[]{}); // from reflection
Method mStatic = c.getDeclaredMethod("testStatic");
mStatic.setAccessible(true);
Object oStatic = mStatic.invoke(A.class, new Object[]{});
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
}


2. By using Class.forName() method.
 package reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {

private void test() {
System.out.println("from test");
}

public static void main(String[] args) {
// TODO code application logic here
try {
Class c = Class.forName("reflection.Main");
Method m = c.getDeclaredMethod("test");
Object o = c.newInstance();
m.invoke(o);

} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
}

Download Source Code
Click here to download complete source code
Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

8 comments:

  1. From the above statistics, it is evident that the size of an organization played a role in the determination of the amount of returns received. It beats logic for one to expect a large organization to receive low amounts of returns and vice versa. SEO Antwerpen

    ReplyDelete
  2. This is the reason online marketing specials efforts so you can effective analyze earlier than post. Put simply to write down more beneficial destination of this nature. Fateh Fashion

    ReplyDelete
  3. Such a device is beneficial because it reduces the stress that comes about when a machine stops running because something became lose or worn out. There is also reduced maintenance cost since it is less prone to wear. This generally makes the device very easy to handle for a person with very little skills in handling a solar panel. www.zonnepanelen-soloya.nl/service/zonnepanelen-kopen

    ReplyDelete
  4. Internet marketing is still essential to any business's overall marketing strategy. Mentioned before with all the benefits of internet marketing, people still buy magazines, billboards went out to check to see their mail, watch TV, and shopping. SEO zoekmachine optimalisatie

    ReplyDelete
  5. Much obliged for Libid Xtreme Caps Valor sharing the data, keep doing awesome... I truly delighted in investigating your site. great asset... Libid Xtreme Caps Bula

    ReplyDelete
  6. A good blog always comes up with new and exciting information and while reading I have felt that this blog has all those qualities that qualify a blog to be one.Best Website Development Company in Kanpur

    ReplyDelete
  7. Go live on social media at least once a week! And be sure to include a call to action that sells your audience something valuable at the end facebook marketing

    ReplyDelete
  8. This is truly a decent and enlightening, containing all data furthermore greatly affects the new innovation. A debt of gratitude is in order for sharing it visit hereDigital Marketing Shahina

    ReplyDelete