• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Accessing Private method outside the class

July 31, 2012 By j2eereference 4 Comments

Accessing Private method outside the class using reflection

Consider a situation where we need to test the private method of class. We cannot directly access this by creating object of the class.

Only way to access the private methods and variables from outside the class using reflection.

Lets see this with the below example.

We can create a class called privateTest which has a private method called display.

1
2
3
4
5
6
7
public class PrivateTest {
 
private void display()
{
System.out.println("This is from display method");
}
}

Now lets see how can we access the private method from outside by using reflection.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
public class Test {
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, SecurityException, NoSuchMethodException {
 
PrivateTest privateTest= new PrivateTest();
Method privateMethod = PrivateTest.class.getDeclaredMethod("display");
privateMethod.setAccessible(true);
privateMethod.invoke(privateTest);
}
 
}

 

output

This is from display method

By default reflection will not allow accessing private methods , To make it happen we have to use the method setAccessible with value as true;

The below method in the mentioned example.

privateMethod.setAccessible(true);

 

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Comments

  1. Asif says

    August 1, 2012 at 10:50 am

    Awesome!!!!!!!!!!!!!! article…

    Reply
  2. Arbuda says

    August 1, 2012 at 12:06 pm

    Can u please post the example to access the parametrized private methods through reflection

    Reply
    • admin says

      August 1, 2012 at 1:17 pm

      public class PrivateTest {

      private void display(String name)
      {
      System.out.println(“You have passed the parameter : “+name);
      }
      }
      ————————————————————————————
      import java.lang.reflect.InvocationTargetException;
      import java.lang.reflect.Method;

      public class Test {
      public static void main(String[] args) throws IllegalArgumentException,
      IllegalAccessException, InvocationTargetException,
      SecurityException, NoSuchMethodException {

      PrivateTest privateTest = new PrivateTest();
      Class[] arg1 = { String.class };
      Method privateMethod = PrivateTest.class.getDeclaredMethod(“display”,
      arg1);
      privateMethod.setAccessible(true);
      privateMethod.invoke(privateTest, “j2eereference.com”);

      }

      }

      Output

      You have passed the parameter : j2eereference.com

      Reply
  3. bs.mohankumar@gmail.com says

    August 1, 2012 at 1:20 pm

    Very nice man .:)

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.