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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:59:41+00:00 2026-05-28T15:59:41+00:00

Not sure if I am asking a sound question. I know every inner class

  • 0

Not sure if I am asking a sound question. I know every inner class keeps a reference to the enclosing class object. So can I reference the enclosing class object outside that enclosing class given the anonymous inner class as function parameter?

class A{
    public static void foo(Thread t) {
        //here, how to get the reference of the enclosing class object of "t", 
        //i.e. the object of class B. I know, somebody might have put in a 
        //non-inner class Thread. But how can I check if "t" is really from an 
        //anonymous inner class of class B and then do something here?
    }
}
class B{
    public void bar() {
        A.foo(
        new Thread() {
            public void run() {
                System.out.println("inside bar!");
            }
        });
    }
}
  • 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-05-28T15:59:42+00:00Added an answer on May 28, 2026 at 3:59 pm

    For getting the enclosing class of t try t.getClass().getEnclosingClass(). Note that this would return null if there was no enclosing class.

    Getting the correct instance of the enclosing class is not that easy, since this would rely on some
    undocumented implementation details (namely the this$0 variable). Here’s some more information: In Java, how do I access the outer class when I'm not in the inner class?

    Edit: I’d like to reemphasize that the this$0 approach is undocumented and might be compiler dependent. Thus please don’t rely on that in production or critical code.

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

Sidebar

Related Questions

I'm not sure I'm asking the right question here, but I'm looking to provide
I'm not sure whether I'm asking the question correctly, but I've been told SQL
Not sure if the title is quite right for the question but I can't
I've googled around but i'm not sure i am asking the right question or
I'm not sure if what I'm asking is possible. You know how when you
I'm not sure what is the best approach for asking these question, so I'm
I know, the title sucks... not sure how to explain what I'm asking, so
I am not sure I am asking the right question here. I have a
I am not sure whether I am asking a dumb question again or not
I'm not sure if I have the jargon for asking this question not being

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.