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

  • Home
  • SEARCH
  • 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 8071239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:39:09+00:00 2026-06-05T13:39:09+00:00

How can I access a private field of an inner class in another package

  • 0

How can I access a private field of an inner class in another package which extends another inner class in yet another package by using reflection (package java.lang.reflect)?

package MainPackage;
import java.lang.reflect.Field;

class PrimaryClass
{
    public static void main(String[] args)
    {
        AuxiliaryPackage.TerciaryClass.BugattiVeyron bugattiVeyron = 
            (new AuxiliaryPackage.TerciaryClass()).new BugattiVeyron(
                new MainPackage.SecondaryClass());        

        System.out.println("Name: "+ bugattiVeyron.name);
        System.out.println("Tires type: " + bugattiVeyron.tiresType);
        try
        {
            Class bugattiVeyronClass = bugattiVeyron.getClass();
            Field invisibleField1 = bugattiVeyronClass.getDeclaredField("topSpeed");
            invisibleField1.setAccessible(true);
            System.out.println(invisibleField1.get(bugattiVeyron));
            Field invisibleField2 = bugattiVeyronClass.getDeclaredField("tamperedOdometer");
            invisibleField2.setAccessible(true);
            System.out.println(invisibleField2.get(bugattiVeyron));
        }
        catch (Exception e)
        {
            /* I ALWAYS GET AN EXCEPTION !! */
            /* I need a change to be done IN the TRY block. */
            /* WHEN THE BugattiVeyron class DOES NOT EXTEND another inner class, NO EXCEPTION. */
            /* See at the end for a detail of the just mentioned case. */
            System.out.println("An exception has occurred.");
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }
}



package MainPackage;
public class SecondaryClass
{
    public class SportsCar
    {
        /* Public fields. Accesible from any package. */
        public String name = new String();

        /* Protected fields. Accesible from the same package and subclasses in any package */
        protected String tiresType = new String();

        /* Package fields. No explicit modifier. Accessible from the same package only. */
        int topSpeed; //km per hour

        /* Private fields. Accessible from the same class only. */
        private boolean tamperedOdometer;

        /* Default constructor */
        public SportsCar()
        {
            name = "Sports car";
            tiresType = "Race";
            topSpeed = 250;
            tamperedOdometer = false;
        }

        public SportsCar(String name, String tiresType, int topSpeed, boolean tamperedOdometer)
        {
            this.name = name;
            this.tiresType = tiresType;
            this.topSpeed = topSpeed;
            this.tamperedOdometer = tamperedOdometer;
        }     
    }
}



package AuxiliaryPackage;
public class TerciaryClass
{
    public class BugattiVeyron extends MainPackage.SecondaryClass.SportsCar
    {
        /* Default constructor */
        public BugattiVeyron(MainPackage.SecondaryClass secondaryClass)
        {
            secondaryClass.super("Buggati Veyron", "Michelin Pilot Sport PS2 PAX", 431, false);
        }
    }
}

When the BugattiVeyron class does not extend anything (after a few changes in the code) I instantiate it like this:

AuxiliaryPackage.TerciaryClass.BugattiVeyron bugattiVeyron = (new
        AuxiliaryPackage.TerciaryClass()).new BugattiVeyron();

… and no exception rises… So what should I change in the try block to get things done well?

Thanks in advance!!

EDIT 1.
I didn’t mean to yell. I thought that the intro might get lost because of the length of the post. I have included the stackTrace. I apologize. This is my second question.

Here is the first exception (The other one is similar):

java.lang.NoSuchFieldException: topSpeed
        at java.lang.Class.getDeclaredField(Unknown Source)
        at MainPackage.PrimaryClass.main(PrimaryClass.java:17)

The line 17 is:
Field invisibleField1 = bugattiVeyronClass.getDeclaredField(“topSpeed”);

EDIT 2.
I know it does NOT look like a “good programming pattern”. This is about a research on the limits of java.lang.reflect.

EDIT 3: THE ANSWER.
I want to thank all those who were kind and patient to me and, tried to provide answers. Finally, I got the solution from vsb. So, it took around an hour to get the answer since I posted the question. A record! My first question has not received any attention at all since March the 1st.

  • 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-05T13:39:10+00:00Added an answer on June 5, 2026 at 1:39 pm

    This code works.

            Class bugattiVeyronClass = bugattiVeyron.getClass();
            Class sportsCarClass = bugattiVeyronClass.getSuperclass();
            Field invisibleField1 = sportsCarClass.getDeclaredField("topSpeed");
            Field invisibleField2 = sportsCarClass.getDeclaredField("tamperedOdometer");
    

    BugattiVeyron class doesn’t have these fields, they are from superclass and must be accessed correspondingly.

    You can use following method to find field in the class hierarchy:

    private static Field getDeclaredFieldInHierarchy(Class<?> clazz, String fieldName) {
        if (clazz == null) {
            return null;
        }
        try {
            return clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            return getDeclaredFieldInHierarchy(clazz.getSuperclass(), fieldName);
        }
    }
    

    But please consider another way to solve your problem. It’s very bad style and should only be used when absolutely necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can hibernate can access a private field/method of a java class , for
my problem is that, i want to save class object java.lang.reflect.Field into database using
How can I access all the member field of the class which contains the
I want to set the value of a private field using reflection for unit
Possible Duplicate: Java private field access I just observed little weird(imho) thing in java
I can access Spring beans in my Servlets using WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); in
We can access the valueStack (and other objects of ActionContext ) using OGNL but
As you know anyone can access strings in an native application using a hex
I ran into a problem that I need to access to private field of
Let see at this class: public class Cluster { private List<Point> points; //private field

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.