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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:07:03+00:00 2026-05-16T03:07:03+00:00

Why doesn’t this code compile? public class A { public class B extends A

  • 0

Why doesn’t this code compile?

public class A {
    public class B extends A {
        public B(A a) { }
    }
    void foo() {
        A a = new A();
        new B(a) { };
    }
}

A.java:[7,17] cannot reference this before supertype constructor has been called

Compilation is successful if either of these changes are made:

  • B is private instead of public
  • line 7 reads new B(A); instead of new B(A) { }

Using javac version: 1.6.0_20

  • 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-16T03:07:04+00:00Added an answer on May 16, 2026 at 3:07 am

    It should be noted that Eclipse, javac, and Intellij IDEA exhibit differences in behaviors with regards to these snippets. javac and the Java Puzzlers behavior is used for reference in this discussion.

    I was able to cut down the snippet to the following:

    public class A {
        class B extends A {
        }
        void foo() {
            new B() { }; // DOES NOT COMPILE!!
        }
    }
    

    This scenario is discussed in Java Puzzlers, Puzzle 90: It’s Absurd, It’s a Pain, It’s Superclass!

    The snippet given is the following:

    public class Outer {                   // "A"
        class Inner1 extends Outer  {}     // "B"
        class Inner2 extends Inner1 {}     // "B" anonymous
    }
    // DOES NOT COMPILE!!
    

    The problem is that due to how default constructor is defined, we really have the following:

    // Same as above but with default constructor included explicitly
    public class Outer {
        class Inner1 extends Outer  { 
            Inner1() { super(); }
        }
        class Inner2 extends Inner1 {
            Inner2() { super(); }    
        }
    }
    // STILL DOES NOT COMPILE!!
    

    The problem is that Inner2‘s superclass is itself an inner class Inner1, thus making Inner2‘s default constructor illegal since it requires an enclosing instance to be supplied to the constructor.

    The "brute-force" way to fix the problem is to provide this explicitly with a qualified-this expression:

    // "brute-force" fix
    public class Outer {
        class Inner1 extends Outer  { 
            Inner1() { super(); }
        }
        class Inner2 extends Inner1 {
            Inner2() { Outer.this.super(); }    
        }
    }
    // NOW COMPILES!
    

    However, the puzzle prescribes that such complicated situation is best avoided in the first place. Here are some quotes:

    This compiles, but it is mind-numbingly complex. There is a better solution: Whenever you write a member class, ask yourself, Does this class really need an enclosing instance? If the answer is no, make it static. Inner classes are sometimes useful, but they can easily introduce complications that make a program difficult to understand. They have complex interactions with generics (Puzzle 89), reflection (Puzzle 80), and inheritance (this puzzle). If you declare Inner1 to be static, the problem goes away. If you also declare Inner2 to be static, you can actually understand what the program does: a nice bonus indeed.

    In summary, it is rarely appropriate for one class to be both an inner class and a subclass of another. More generally, it is rarely appropriate to extend an inner class; if you must, think long and hard about the enclosing instance. Also, prefer static nested classes to non-static. Most member classes can and should be declared static.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unless you have some really strange dependencies, it won't compile… May 16, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer I have tried http://www.codesynthesis.com/products/xsd/ a few months back and I… May 16, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer OK. Just found out that there is nothing wrong with… May 16, 2026 at 5:31 pm

Trending Tags

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

Top Members

Related Questions

Why doesn't this code compile? template <class T> class A { public: A(T t)
Why doesn't this work? - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { if (TRACE_LOG) NSLog(@%s, __FUNCTION__);
Why doesn't that work in java, but this does Map<String, Map<String, Boolean>> myMap =
This doesn't work panel1.layout: layout [ offset: 0x0 yuml-image: image img ] panel2.layout: layout
Why doesn't this work?? I am trying to create an onClickListener for a button
Why doesn't Java need operator overloading? Is there any way it can be supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm looking for suggestions for debugging... If you view this site in Firefox or

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.