Creating an object of the inner class

In my book it says..

If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName.

Does this mean that there is a way to create an object of the inner class from a separate class to the outer class and its inclosing classes other than this way?

class separate{

Outer outer =new Outer();

Outer.Inner inner = outer.method();

}

publicclass Outer{

Inner method(){

returnnew Inner();

}

class Inner{

}

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

}

}

thanks

[1420 byte] By [brettosm8a] at [2007-11-27 1:23:38]
# 1

The way you did it (with the factory method "method") is the way most people

prefer to do it, but you can also use this funky syntax:

class separate {

Outer outer = new Outer();

Outer.Inner inner2 = outer.new Inner(); //good gracious!

}

DrLaszloJamfa at 2007-7-12 0:13:31 > top of Java-index,Java Essentials,New To Java...
# 2

class Separate {

Outer.Inner inner = new Outer().new Inner();

}

class Outer {

class Inner {}

}

prometheuzza at 2007-7-12 0:13:31 > top of Java-index,Java Essentials,New To Java...
# 3

Often, one of the reasons Inner is an inner class is because its existence

is an implementation detail. What's important is the interface it

implements or the class it extends. Then it is declared private, and

your code looks like this:

class Separate {

Outer outer = new Outer();

Runnable runner = outer.createRunnable();

}

public class Outer {

public Runnable createRunnable(){

return new Inner();

}

private class Inner implements Runnable {

public void run() {

}

}

}

Note how clean is the code in Separate. You could change the implemation

of creareRunnable and/or Inner in many ways without having to change

the code in Separate.

DrLaszloJamfa at 2007-7-12 0:13:31 > top of Java-index,Java Essentials,New To Java...
# 4
thanks a lot for the tips.
brettosm8a at 2007-7-12 0:13:31 > top of Java-index,Java Essentials,New To Java...