-4

I am learning about the Inner Class in Java, but i write this code, when i compile it, i got this error: Error:(84, 23) java: non-static variable this cannot be referenced from a static context.

    public class Main {

    class Outer {
        private String string;

        Outer(String s) {
            this.string = s;
        }

        public Inner inner() {
            return new Inner();
        }

        class Inner {
            @Override
            public String toString() {
                return string;
            }
        }
    }

    public static void main(String[] args) {
        Outer outer = new Outer("yang");
      //  System.out.println(outer.inner().toString());
    }
}

I have update the Inner Class to this, and it works.

public class Main {

    static class Outer {
        private String string;

        Outer(String s) {
            this.string = s;
        }

        public Inner inner() {
            return new Inner();
        }

        class Inner {
            @Override
            public String toString() {
                return string;
            }
        }
    }
    public Outer outer(String s){
        return new Outer(s);
    }
    public static void main(String[] args) {
      //  Main main = new Main();
        Outer outer = new Outer("YANG");
        System.out.println(outer.inner().toString());
    }
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • `System.out.println(outer.inner().toString());` Are you getting the error after removing the comment from this line ?? – Code Geek Jan 01 '15 at 03:18
  • No, The error is in `Outer outer = new Outer("yang");` – BlackMamba Jan 01 '15 at 03:20
  • Objects that are instances of an `Outer class` exist within an instance of the Main class. so you needs to create instance of Main class to access `Outer class`. – bNd Jan 01 '15 at 03:25
  • @BhumikaThaker The only 'outer class' here is `Main`. Do you mean 'instances of the `Outer` class' and 'to access [an instance of the] `Outer` class'? – user207421 Jan 01 '15 at 03:26
  • Yes I mean Outer class is equal to `Main`. as oops gives name of inner class as `Outer`. – bNd Jan 01 '15 at 03:29
  • @BlackMamba Please re-edit your question or else users might be confused with your question and you may attract down votes – Code Geek Jan 01 '15 at 03:31
  • @BhumikaThaker You are still perpetating the confusion between `Outer` and 'outer'. Writing 'Outer class is equal to `Main`' rather than 'outer class is equal to `Main`', and '*an* `Outer` class' rather than '*the* `Outer` class', only causes further confusion. Please be accurate. – user207421 Jan 01 '15 at 03:35

2 Answers2

5

Despite its name, Outer is itself an inner class, and therefore cannot be constructed without an instance of Main. You need:

Outer outer = new Main().new Outer("yang");
user207421
  • 305,947
  • 44
  • 307
  • 483
1

Both Outer and Inner are inner classes. To access them you must instantiate the main class and then you should instantiate the inner class.

So your code will be

Outer outer = new Main().new Outer("Yang");

Also refer to this

Code Geek
  • 485
  • 1
  • 5
  • 15