What are rules to observer when writting More than one class in single Java file.
Let say three classes on the same file

Thansk

First rule: don't do it unless you're using inner classes
Second rule: see first rule

Third rule: if you insist, read the JLS for what's allowed and what's not.

First rule: don't do it unless you're using inner classes
Second rule: see first rule

Third rule: if you insist, read the JLS for what's allowed and what's not.

Ok ;)

What is JLS? and what are inner classes?

if you don't know what inner classes are, I doubt you're using them.
in which case, I would recommend you to follow JWentings advice:
DO NOT put two classes in one single Java file

JLS: Java Language Specification :)

ok I will not,
but what are drawbacks?

You can write any number of classes in a single Java program. Suppose I've written three classes A, B and C in the same java file. Then any one of these classes will have the main method. Suppose class C has the main method inside it. Then the name of that program must be C.java and preferably it has to be public.

The drawbacks are mainly in code maintainability.
Everyone expects to find 1 class per source file, including most IDEs.

See the filename, you know what class is contained in it and vice versa.
Want to change a class? Change that file and all other classes are not touched. Ideal for version control purposes.

I know this has been marked as solved, but thought i'd chime in.

Completely agree with Mr. jwenting. The only effect is on the readability of code and code maintainability. The purpose of including multiple classes (non-public top-level, nested and inner classes) in one source file is to bundle related support functionality together. The public class must be implemented in a file with the same name as the class. A single .java file can contains one public and optionally some non-public top level classes, public nested and inner classes. This is useful if the classes are only used internally by the public class.

I recommend to read - http://mindprod.com/jgloss/nestedclasses.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.