Home » Java » Core Java » Calling private Java methods publicly?

About Ronald Djunaedi

Ronald Djunaedi
Ronald is a senior Java Developer, specialization in J2EE Programming and its technologies and frameworks. Been a trainer for a while, specialization in training subjects: Java Fundamentals, OOP and J2EE. Has been involved actively in numerous IT Software Development Projects with multiple companies and clients.

Calling private Java methods publicly?

We Java developers, known 4 access modifiers in Java: private, protected, public, and package. Well, except for the private, the last three, can be called from outside of the class by inheritance, same package or from the instance.

Now, the common question, can private be called publicly (from outside class)? well the answer is NO and YES. No when you use ‘usual’ way to access it, and YES when you ‘hack’ into it using the Reflection API provided by Java itself.

Well okay, now just write the code that we will hack into. I called it as “TheVictim

package com.namex.hack;

public class TheVictim {
 private void hackTest() {
  System.out.println("hackTest called");
 }

 private static void hackTestStatic() {
  System.out.println("hackTestStatic called");
 }

}

Now after that, just follow my code and try to run it. I guarantee that if you followed it right, you will get TheVictim to call both of the hackTest and hackTestStatic. And you can see the output on your screen.

package com.namex.hack;

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

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

  Class c = TheVictim.class;

  Method[] ms = c.getDeclaredMethods();

  for (Method each : ms) {
   String methodName = each.getName();
   each.setAccessible(true); // this is the key
   if (Modifier.isPrivate(each.getModifiers())) {
    
    if (Modifier.isStatic(each.getModifiers())) {
     // static doesnt require the instance to call it.
     each.invoke(TheVictim.class, new Object[] {});
    } else {
     each.invoke(new TheVictim(), new Object[] {});
    }
   }
  }

 }
}

Output example:

hackTestStatic called
hackTest called

Okay, this tutorial has met its purpose. Now you know the Reflection API of java is very powerful feature of programming language. And it’s all up to you to modify or even extend it for your own purpose. Have fun with Java :)

Reference: Calling private methods publicly ? from our JCG partner Ronald Djunaedi at the Naming Exception blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


7 × = twenty one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.