Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8223643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:46:45+00:00 2026-06-07T14:46:45+00:00

I know that we can access private constructor via reflection as @Sanjay T. Sharma

  • 0

I know that we can access private constructor via reflection as @Sanjay T. Sharma mentioned in his answer of my question: Does “instanceof Void” always return false?

However, @duffymo said:

you can access private everything with reflection – methods, constructors, data members, everything.

  1. How can I access the private methods and the private data members?
  2. Is it possible to access local variable via reflection?
  3. Is there a way to prevent anyone from accessing private constructors, methods, and data members?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:46:46+00:00Added an answer on June 7, 2026 at 2:46 pm

    1) How can I access the private methods and the private data members?

    You can do it with a little help of the setAccessible(true) method:

    class Dummy{
        private void foo(){
            System.out.println("hello foo()");
        }
        private int i = 10;
    }
    
    class Test{
        public static void main(String[] args) throws Exception {
            Dummy d = new Dummy();
    
            /*---  [INVOKING PRIVATE METHOD]  ---*/
            Method m = Dummy.class.getDeclaredMethod("foo");
            //m.invoke(d); // Exception java.lang.IllegalAccessException
            m.setAccessible(true);//Abracadabra
            m.invoke(d); // Now it's OK
    
            /*---  [GETING VALUE FROM PRIVATE FIELD]  ---*/
            Field f = Dummy.class.getDeclaredField("i");
            //System.out.println(f.get(d)); // Not accessible now
            f.setAccessible(true); // Abracadabra
            System.out.println(f.get(d)); // Now it's OK
    
            /*---  [SETTING VALUE OF PRIVATE FIELD]  ---*/
            Field f2 = Dummy.class.getDeclaredField("i");
            //f2.set(d,20); // Not accessible now
            f2.setAccessible(true); // Abracadabra
            f2.set(d, 20); // Now it's OK
            System.out.println(f2.get(d));
        }
    }
    

    2) Is it possible to access a local variable via reflection?

    No. Local variables cannot be accessed outside of a block in which they were created (someone could say that you can assign such a variable to a field like field = localVariable; and later access such a field via reflection, but this way we will be accessing the value, not the variable).

    3) Is there any way to prevent anyone from accessing private constructors, methods, and data members?

    I think for constructors or methods you could use stacktrace to check if it was invoked by Reflection.

    For fields I can’t find a solution to prevent accessing them via reflection.

    [WARNING: This is not approved by anyone. I just wrote it inspired by your question.]

    class Dummy {
        private void safeMethod() {
            StackTraceElement[] st = new Exception().getStackTrace();
            // If a method was invoked by reflection, the stack trace would be similar
            // to something like this:
            /*
            java.lang.Exception
                at package1.b.Dummy.safeMethod(SomeClass.java:38)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            ->    at java.lang.reflect.Method.invoke(Method.java:601)
                at package1.b.Test.main(SomeClass.java:65)
            */
            //5th line marked by "->" is interesting one so I will try to use that info
    
            if (st.length > 5 &&
                st[4].getClassName().equals("java.lang.reflect.Method"))
                throw new RuntimeException("safeMethod() is accessible only by Dummy object");
    
            // Now normal code of method
            System.out.println("code of safe method");
        }
    
        // I will check if it is possible to normally use that method inside this class
        public void trySafeMethod(){
            safeMethod();
        }
    
        Dummy() {
            safeMethod();
        }
    }
    
    class Dummy1 extends Dummy {}
    
    class Test {
        public static void main(String[] args) throws Exception {
            Dummy1 d1 = new Dummy1(); // safeMethod can be invoked inside a superclass constructor
            d1.trySafeMethod(); // safeMethod can be invoked inside other Dummy class methods
            System.out.println("-------------------");
    
            // Let's check if it is possible to invoke it via reflection
            Method m2 = Dummy.class.getDeclaredMethod("safeMethod");
            // m.invoke(d);//exception java.lang.IllegalAccessException
            m2.setAccessible(true);
            m2.invoke(d1);
        }
    }
    

    Output from Test main method:

    code of safe method
    code of safe method
    -------------------
    Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at package1.b.Test.main(MyClass2.java:87)
    Caused by: java.lang.RuntimeException: method safeMethod() is accessible only by Dummy object
        at package1.b.Dummy.safeMethod(MyClass2.java:54)
        ... 5 more
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know if you can access the SaevFileDialog control that's used by the
I know that the derived class can't access the private members of the base
Can anyone let me know how can access an element of a list that
I know that no thread can access the current view unless it is the
I know that can co-exists with web form no problem as Hanselman mention here
I know that it's a subject that can raise a lot of debate, but
I want to know that can mouse click event detects colors or even strings.
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I know that I can get word completion through CTRL+N & CTRL+P and code
I know that this can be easily done by using if(i%5 == 0 OR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.