4

Please excuse me if this question has been asked before. I have searched around the web and here at stack overflow.

Case:

I am stuck at my java project and the compiler won't compile my project when I try to use an inner class inside another.

Code:

public class outer  {
    public class middle {
        public class inner  {
            public int variable = "8";
        }
    }
}

Declaration:

I am trying to declare the classes like this:

outer outerObject = new outer();
outerObject.middle middleObject = outerObject.new middle();
outerObject.middleObject.inner innerObject = outerObject.middleObject.new inner();

Compilation results:

source\start.java:8: error: cannot find symbol
                      outerObject.middleObject.inner innerObject = outerObject
.middleObject.new inner();
^

Any reply would be much appreciated! Wether my intentions are impossible or if I am simply doing something wrong.

Have a nice day!

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • check this link http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class?rq=1 –  Apr 22 '13 at 12:41

4 Answers4

4

The line

outerObject.middleObject.inner innerObject = outerObject.middleObject.new inner();

should be

outer.middle.inner innerObject = middleObject.new inner();

outerObject does not have a middleObject field. Unfortunately Java allows statics to be qualified with variable expressions, so you get some strange code actually compiling.

This would probably be clearer if you stuck with the Java naming conventions.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
2

Your code will never compile as:

public int variable = "8";

This line should be

public int variable = 8;

First change this line.

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

use this

public class outer  {
    public class middle {
        public class inner  {
            public int variable = 8;
        }
    }
    public static void main(String[] a) {
        outer outerObject = new outer();
        middle middleObject = outerObject.new middle();
        middle.inner innerObject = middleObject.new inner();
    }
}
NPKR
  • 5,368
  • 4
  • 31
  • 48
0

Please try this :

    Outer outerObject = new Outer();
    Outer.middle middleObject = outerObject.new middle();
    Outer.middle.inner innerObject = middleObject.new inner();

Also you need to change:

 public int variable = "8";

to

 public String variable = "8";
AllTooSir
  • 48,828
  • 16
  • 130
  • 164