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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:49:44+00:00 2026-05-30T01:49:44+00:00

Are there any circumstances in which Class.getDeclaringClass could give a different result from Class.getEnclosingClass

  • 0

Are there any circumstances in which Class.getDeclaringClass could give a different result from Class.getEnclosingClass?

I thought it may be to do with a subclass of the outer class instantiating an inner class that was not declared as static, but I wasn’t able to get a difference that way:

public class Main {
  private static class StaticInnerClass {

  }

  private class MemberInnerClass {

  }

  private static class ChildClass extends Main {

  }

  public MemberInnerClass getMemberInnerClassInstance() {
    return new MemberInnerClass();
  }

  public static void main(String[] args) {
    System.out.println( StaticInnerClass.class.getDeclaringClass() );
    System.out.println( StaticInnerClass.class.getEnclosingClass() );
    System.out.println( MemberInnerClass.class.getDeclaringClass() );
    System.out.println( MemberInnerClass.class.getEnclosingClass() );
    System.out.println( new ChildClass().getMemberInnerClassInstance().getClass().getEnclosingClass() );
    System.out.println( new ChildClass().getMemberInnerClassInstance().getClass().getDeclaringClass() );
  }
}

Output:

class Main
class Main
class Main
class Main
class Main
class Main
  • 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-30T01:49:45+00:00Added an answer on May 30, 2026 at 1:49 am

    Found here http://kickjava.com/1139.htm#ixzz1mv2nEWg7:

    “The subtilty with getDeclaringClass is that anonymous inner classes are not counted as member of a class in the Java Language Specification whereas named inner classes are. Therefore this method returns null for an anonymous class. The alternative method getEnclosingClass works for both anonymous and named classes.”

    For example:

    public class Test {
        public static void main(String[] args) {
            new Object() {
                public void test() {
                    System.out.println(this.getClass().getDeclaringClass()); //null
                    System.out.println(this.getClass().getEnclosingClass()); //not null
                }
            }.test();
        }
    }
    

    The same holds for non-anonymous classes in a method scope:

    class Foo {
      Class<?> bar() throws NoSuchFieldException {
        class Bar<S> { }
        return Bar.class;
      }
    
      static void main(String[] args) throws NoSuchFieldException {
        System.out.println(new Foo<Void>().bar().getDeclaringClass()); // null
        System.out.println(new Foo<Void>().bar().getEnclosinglass()); // Foo
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any circumstances in which it is legitimate for a derived class to
As a general rule, are there ever any circumstances in which it's acceptable for
Are there any circumstances under which a ThreadPoolExecutor.CallerRunsPolicy will throw a RejectedExecutionException ? It
Are there any circumstances where it is favourable to manually create a stub type,
Is there any query which can return me the number of revisions made to
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
I have a class derived from CTreeCtrl. In some circumstances the user might be
Is there any way to check whether a file is locked without using a
Is there any free or commercial component written in .NET (no COM interop) that
Is there any efficiency difference in an explicit vs implicit inner join? For example:

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.