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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:57:08+00:00 2026-06-14T01:57:08+00:00

Consider the following: public class OuterClass { private String attribute = outer; class InnerClass

  • 0

Consider the following:

public class OuterClass {

    private String attribute = "outer";

    class InnerClass {
        private String attribute = "inner";
        public doSomething() {
            System.out.println(this.attribute);
            System.out.println(OuterClass.this.attribute);

        }
    }

}

The InnerClass is not static and must be created against an instance of it’s outer class.

new OuterClass().new InnerClass()

The regular innerclass holds a reference to the outer class in which it was created, that is accessible using Outer.this.myAttribute (particularly useful in this case where there is a “naming colision”


When creating an anonymous innerclass, it’s the same: the anonymous innerclass created holds a reference to the outer class, this is why when declaring a predicate inside a method (anonymous method-local innerclass), we can still access, inside the innerclass, the variables of the outerclass without having to declare them final (while we should for variables passed as method parameters.

public class OuterClass {

  // Do not need to be final because the innerclass keeps a reference to the outerclass
  // even if it's an anonymous innerclass, it's still an innerclass
  private String classAttribute = "classAttribute";

  public Runnable doSomething() {

    // Should be final because the variable lives on the stack and the reference to this object
    // must be copied so that the String object is still accessible when the stack frame is destroyed
    final String localVar = "localVar";

    return new Runnable() {
      @Override
      public void run() {
        System.out.println(classAttribute);
        System.out.println(localVar);
      }
    };
  }

}

And finally, we can declare constants in an interface, which are implicitly marked public static final. An object can be a constant.
Thus an object created as an anonymous innerclass is a legal constant for an interface.

For exemple, when using Guava, I usually declare in my interface functions and predicates which permits me to leverage the useful Guava functions like Maps.uniqueIndex(...).

public interface AlternativeNameable {

  String getAlternativeName();

  Function<AlternativeNameable,String> GET_ALTERNATIVE_NAME = new Function<AlternativeNameable,String>() {
    @Override
    public String apply(AlternativeNameable input) {
      return input.getAlternativeName();
    }
  };

}

So you may ask yourself what is my question? Here it is:

When declaring an anonymous class as an interface constant (see my last code sample), on which outerclass does the anonymous innerclass holds a reference to?

  • 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-14T01:57:10+00:00Added an answer on June 14, 2026 at 1:57 am

    Inner classes of interfaces are implicitly static and as such, do not require reference to outer class.

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

Sidebar

Related Questions

Consider the following: public class GenericTest { static void print(int x) { System.out.println(Int: +
Consider you have the following class public class OuterClass { ... private static class
Consider the following code: public partial class TeacherControlPanel : System.Web.UI.Page { protected string username
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following code : public class RandomClass { private readonly string randomString; public
Consider the following Java code: public class Test { private Foo< String, String >
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string
Please consider the following code: public class Person ( public string FirstName {get; set;}
Consider the following class hierarchy: public class Foo { public string Name { get;
Consider the following nested type: public class Book { private class Chapter { }

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.