Dec0de
Dec0de

Reputation: 401

Cannot be referenced from static context error

I have two classes inside Main class. I want to instantiate both objects of that classes in static main method. But i'm getting "cannot be referenced from static context error". Where i'm wrong?

public class Main {

    public static void main(String[] args) {
        Parent p = new Parent();
    }

    public class Parent {
        String x = "Parent";
        public void print() {
            System.out.println(x);
        }
    }

    public class Child extends Parent {
        String x = "Child";
        public void print() {
            System.out.println(x);
        }
    }

}

Upvotes: 1

Views: 112

Answers (2)

Jose Alberto
Jose Alberto

Reputation: 46

Parent and Child classes depend on an instance of Main because they are inner classes.

If you want to create a Parent or Child instance, you can do two things.

  • Create an instance of Main and then use this reference to create Parent or Child.
    public class Main {
        public static void main(String[] args) {
            Main m = new Main();
            Main.Parent p = m.new Parent();
        }

        public class Parent {
            String x = "Parent";
            public void print() {
                System.out.println(x);
            }
        }

        public class Child extends Parent {
            String x = "Child";
            public void print() {
                System.out.println(x);
            }
        }    
    }

  • Change class declaration of Parent and Child and add static
    public class Main {    
        public static void main(String[] args) {
            Parent p = new Parent();
        }

        public static class Parent {
            String x = "Parent";
            public void print() {
                System.out.println(x);
            }
        }

        public static class Child extends Parent {
            String x = "Child";
            public void print() {
                System.out.println(x);
            }
        }

    }

Upvotes: 1

Amit Kumar Lal
Amit Kumar Lal

Reputation: 5789

You can either take the classes out of the main method but its a very messy way and is not suggested at all.

public class SOTest {

    public static void main(String[] args) {
        Parent p = new Parent();
    }
}
class Parent {
    String x = "Parent";
    public void print() {
        System.out.println(x);
    }
}

class Child extends Parent {
    String x = "Child";
    public void print() {
        System.out.println(x);
    }
}

Or one of the best way is to make the Parent & Child class as non static inner class as below

public class SOTest {

    public static void main(String[] args) {
        SOTest soTest = new SOTest();
        Parent p = soTest.new Parent(); //Example of non static inner class
    }

    class Parent {
        String x = "Parent";
        public void print() {
            System.out.println(x);
        }
    }

    class Child extends Parent {
        String x = "Child";
        public void print() {
            System.out.println(x);
        }
    }
}

Or possibly static inner class (depends upon your requirement)

public class SOTest {

    public static void main(String[] args) {
        Parent p = new SOTest.Parent(); //Example of static inner class
    }

    static class Parent {
        String x = "Parent";
        public void print() {
            System.out.println(x);
        }
    }

    static class Child extends Parent {
        String x = "Child";
        public void print() {
            System.out.println(x);
        }
    }
}

Upvotes: 0

Related Questions