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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:23:29+00:00 2026-05-11T14:23:29+00:00

If I have an instance of an inner class, how can I access the

  • 0

If I have an instance of an inner class, how can I access the outer class from code that is not in the inner class? I know that within the inner class, I can use Outer.this to get the outer class, but I can’t find any external way of getting this.

For example:

public class Outer {   public static void foo(Inner inner) {     //Question: How could I write the following line without     //  having to create the getOuter() method?     System.out.println('The outer class is: ' + inner.getOuter());   }   public class Inner {     public Outer getOuter() { return Outer.this; }   } } 
  • 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. 2026-05-11T14:23:29+00:00Added an answer on May 11, 2026 at 2:23 pm

    The bytecode of the Outer$Inner class will contain a package-scoped field named this$0 of type Outer. That’s how non-static inner classes are implemented in Java, because at bytecode level there is no concept of an inner class.

    You should be able to read that field using reflection, if you really want to. I have never had any need to do it, so it would be best for you to change the design so that it’s not needed.

    Here is how your example code would look like when using reflection. Man, that’s ugly. 😉

    public class Outer {     public static void foo(Inner inner) {         try {             Field this$0 = inner.getClass().getDeclaredField('this$0');             Outer outer = (Outer) this$0.get(inner);             System.out.println('The outer class is: ' + outer);          } catch (NoSuchFieldException e) {             throw new RuntimeException(e);         } catch (IllegalAccessException e) {             throw new RuntimeException(e);         }     }      public class Inner {     }      public void callFoo() {         // The constructor of Inner must be called in          // non-static context, inside Outer.         foo(new Inner());      }      public static void main(String[] args) {         new Outer().callFoo();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, you need to add a custom XML namespace that… May 11, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer list.SelectedItem should contain the selected row's DataRowView. Then you can:… May 11, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer Handled this as an exception to the normal string cleansing… May 11, 2026 at 5:08 pm

Related Questions

Say I have the following C# code: Action a = TestMethod; Action b =
I'd like to write a generic method that creates a new instances of a
If I have an instance of a System.Timers.Timer that has a long interval -
So if I have an instance of System.Reflection.Assembly and I have the following model:

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.