every class declared in its own .class file?

For every class you declare, do you put it alone it its own .class file or underneath another one in the same file?

For instance: http://java.sun.com/docs/books/tutorial/java/javaOO/classes.html

public class Bicycle {

// Is this declared in its own file? Can it be under the main one

//in the same file? How does that work?

}

Also how does subclass play into it? Id guess that every class get its own .class file but Subclasses are declared in the same .class file under the bigger one. If not then what?

public class MountainBike extends Bicycle {

}

[607 byte] By [WOPa] at [2007-11-27 9:27:05]
# 1

Most of the time, i've seen subclasses declared in their own file. If they are in the same file with another class, then one of the classes is an inner or nested class which may behave differently than a non-nested class (unless it is....... nah, I'll leave static inner classes out of this discussion).

I guess my recommendation is to not nest your classes without a good reason to do so.

petes1234a at 2007-7-12 22:24:33 > top of Java-index,Java Essentials,New To Java...
# 2
alright then the general rule is just keep every class and subclass its its own file right?
WOPa at 2007-7-12 22:24:33 > top of Java-index,Java Essentials,New To Java...
# 3

Yes, put each class in its own file. The only reason to ever have an inner class (in my opinon) within a class is to assist functions within your class in carrying out some complex task where you want to put some complex data structure (with its own supporting functions) inside the inner class so you can manipulate it more easily by functions in your outer class. That inner class would never be accessible to any other class outside of the containing class. You probably will seldom need to do this (in situations like this, it would be better instead to put the inner class in its own file and make it package level access so only classes of the same package level can see it).

By the way, if you compile a class with an inner class, I believe the compiler will still create a separate file for the inner class, with a name that is similiar to the outer class, but with a '#' character in it.

George123a at 2007-7-12 22:24:33 > top of Java-index,Java Essentials,New To Java...
# 4
anonymous inner classes don't create another .class files tooas said before, DO NOT CREATE nested or anonymous classes, unless you really need to do it.
pbulgarellia at 2007-7-12 22:24:33 > top of Java-index,Java Essentials,New To Java...
# 5
> as said before, DO NOT CREATE nested or anonymous> classes, unless you really need to do it.How do you know when that is?~
yawmarka at 2007-7-12 22:24:33 > top of Java-index,Java Essentials,New To Java...
# 6

> anonymous inner classes don't create another .class

> files too

>

Yes they do.

public class Test121{

private Object someObject = new Object() {};

public static void main(String[] args) {

Object someOtherObject = new Object() {};

}

}

>javac Test121.java

>dir Test121*.*

. . . edited for privacy

324 Test121$1.class

302 Test121$2.class

441 Test121.class

184 Test121.java

Note: This has been this way for a long time; however, I do not know if it is in the specs of just the way some (many?) compilers do it.

Message was edited by:

jbish

jbisha at 2007-7-12 22:24:33 > top of Java-index,Java Essentials,New To Java...