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 9202775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:19:50+00:00 2026-06-17T23:19:50+00:00

Presume we have two different packages… one package can’t be accessed but we like

  • 0

Presume we have two different packages… one package can’t be accessed but we like to know the value of a complex field called b.

public class A {
    private String  whatever;
    private B       b;

    private static class B {
         final ArrayList<Z> c   = new ArrayList<Z>();

         private void addItem(Z z) {
                this.c.add(z);
         }

         private Z getItem(int nr) {
                return this.c.get(nr);
          }
     }
}

public class Reflect extends A {
      public static void main(String[] args) throws NoSuchFieldException, SecurityException {
            Reflect ref = new Reflect();
            Class getA = ref.getClass().getSuperclass();
            Field getB = getDeclaredField("b");
            getB.setAccessible(true);
            Class bInst = getB.getClass();
            Method bMeth = bInst.getMethod("getItem", Integer.TYPE);
            Object zInst = bMeth.invoke(new Integer(123));
      }
}

How can I get the value if I don’t get the complex type B from the package ?
Still get java.lang.NoSuchMethodException: stackOver.A.getItem(int) even I set the field gstB accessible ….

  • 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-17T23:19:51+00:00Added an answer on June 17, 2026 at 11:19 pm

    The only thing you are missing is that getField only gives you public accessible fields.

     Field getB = getA.getDeclaredField("b");
    

    will give you any field of that class.


    A longer example

    class Main {
        public static class A {
            private String whatever;
            private B b = new B();
    
            private static class B {
                final ArrayList<String> c = new ArrayList<String>();
    
                private void addItem(String z) {
                    this.c.add(z);
                }
    
                private String getItem(int nr) {
                    return this.c.get(nr);
                }
            }
        }
    
        public static class Reflect extends A {
            public static void main(String... ignored) throws Exception {
                Reflect ref = new Reflect();
                Class getA = ref.getClass().getSuperclass();
                Field getB = getA.getDeclaredField("b");
                getB.setAccessible(true);
                Object b = getB.get(ref);
    
                Method addItem = b.getClass().getDeclaredMethod("addItem", String.class);
                addItem.setAccessible(true);
                addItem.invoke(b, "Hello");
    
                Method getItem = b.getClass().getDeclaredMethod("getItem", int.class);
                getItem.setAccessible(true);
                String hi = (String) getItem.invoke(b, 0);
                System.out.println(hi);
            }
        }
    }
    

    prints

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

Sidebar

Related Questions

If I have two different threads via GCD accessing an NSMutableArray and one is
Let's presume that I have string like '=&?/;#+%' to be a part of my
I can't see this in the docs, but I presume it's a solved problem.
I have two functions that execute two ajax calls. If I call one function
OK, I've tried but I just don't get it. I have two classes logger
I have two MySQL database tables: one containing a list of championships and another
In my application I have two tables (I have more but I'm only discussing
Can you show me the right way of Mapping if I have two similar
I have two sets (although I can do lists, or whatever): a = frozenset(('Today','I','am','fine'))
I have an app that deals with currency, but obviously I'd like it to

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.