0

I found this code of javatpoint. But when I try to compile this it gives me an error.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    No enclosing instance of type Hierarchical is accessible. Must qualify the allocation with an enclosing instance of type Hierarchical (e.g. x.new A() where x is an instance of Hierarchical).
    No enclosing instance of type Hierarchical is accessible. Must qualify the allocation with an enclosing instance of type Hierarchical (e.g. x.new A() where x is an instance of Hierarchical).
    at Inheritance.Hierarchical.main(Hierarchical.java:28)

Why am I getting this error ?? Eclipse tells me to make the classes static but I don't understand why ??

package Inheritance;

public class Hierarchical {

    static void bayern() {
        System.out.println("bayern");
    }

    class Hierarchical2 extends Hierarchical {

        static void barcelona() {
            System.out.println("barcelona");
        }
    }

    class Hierarchical3 extends Hierarchical {
        static void Madrid() {
            System.out.println("madrid");
        }
    }

    public static void main(String[] args) {
        Hierarchical2 h2 = new Hierarchical2();
        Hierarchical3 h3 = new Hierarchical3();
        h2.barcelona();
        h2.bayern();
        h3.Madrid();
        h3.bayern();
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Junder93
  • 1
  • 1
  • 3
    Possible duplicate of [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class) – Jean-Baptiste Yunès Mar 16 '17 at 11:06

2 Answers2

1

Since Hierarchical2 and Hierarchical3 are not static, you need an instance of the enclosing class (Hierarchical) to instantiate theses classes.

Here how to do it :

Hierarchical hierarchical = new Hierarchical();
Hierarchical2 hierarchical2 = hierarchical .new Hierarchical2();

Declaring classes Hierarchical2 and Hierarchical3 static, you could insantiate the way you did.

Hope it's help.

1

Your class Hierarchical2 is an inner class of Hierarchical, meaning that each instance must be created within an instance of Hierarchical. However, since main() is a static method, there is no instance for the inner class to belong to.

If you move the content of main() into a new non-static method, say public void champions(), and have main do something like

Hierarchical h1 = new Hierarchical();
h1.champions();

That will get rid of your error.

I do wonder if there's much point to what you are doing. Do you have some aim you're trying to achieve? I don't think you will learn much about inheritance from what you are writing.

Andrew McGuinness
  • 2,092
  • 13
  • 18
  • Well my basic aim is to improve my base. I have decided to make any android application as my final year project but my java/advanced java base is very weak although I'm familiar with it. Javatpoint , Tutorialpoint are few of the sites I have referred too. If you can recommend me a better way to learn and improve my command on this language then it would be really helpful. Keeping in mind that I have little external help besides the internet – Junder93 Mar 17 '17 at 12:20