PDA

View Full Version : Java Inner Class Source Code Separation?



Trinithis
09-29-2007, 06:26 AM
Is is possible to define an inner class within a seperate source file from the outer class? If so, what is the syntax for it?

Twey
09-29-2007, 11:18 AM
I don't think so. Inner classes are a property of their outer class, and they're treated the same as any other property. C# has a method for splitting a single class across multiple files, but insofar as I know, Java doesn't. I would suggest, however, that if you need to do so, said class is too big. Break it up more. Inner classes are meant for convenience purposes only. If you end up with a large inner class, consider making it a normal class.

You can always have:
// OuterClass.java

class OuterClass {
public static void main(String[] args) {
System.out.println("Hello!");

InnerClass ic = new InnerClass();
ic.hello();
}
// InnerClass.java

class OuterClass$InnerClass {
public void hello() {
System.out.println("Hi!");
}
}
}... and cat them together.