Java: Multiple Class Declarations in One File

Java: Multiple class declarations in one file

My suggested name for this technique (including multiple top-level classes in a single source file) would be "mess". Seriously, I don't think it's a good idea - I'd use a nested type in this situation instead. Then it's still easy to predict which source file it's in. I don't believe there's an official term for this approach though.

As for whether this actually changes between implementations - I highly doubt it, but if you avoid doing it in the first place, you'll never need to care :)

Multiple classes in single file

One Java file can consist of multiple classes with the restriction that only one of them can be public. As soon as you remove public keyword from your classes, you can combine them into a single Java file.

Can a .java file have multiple Classes and Interfaces?

You can have multiple top-level classes or interfaces inside a Java source file. However, if one of them is public, the file must be named after that class. Therefore you can only have one public class in a Java source file.

Example of a Java source file containing multiple classes and interfaces:

package somepackage;

// note that main classes do NOT need to be public, as some people are saying.
class MainClass {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

// multiple classes
class Class2 {
}

interface Interface1 {
}

// multiple interfaces
interface Interface2 {
}

You can also have inner classes/interfaces (even public ones), nested classes/interfaces (even public ones), and anonymous classes.

Can a java file have more than one class?

Yes, it can. However, there can only be one public top-level class per .java file, and public top-level classes must have the same name as the source file.

The purpose of including multiple classes in one source file is to bundle related support functionality (internal data structures, support classes, etc) together with the main public class. Note that it is always OK not to do this--the only effect is on the readability (or not) of your code.

Can I use multi java class in a same file?

Say you want to have two have 2 classes in one file, you can do this:

public class Main 
{
public static void main(String[] args)
{

Human bob = new Human("bob");
System.out.println(bob.name);
}

}

class Human
{
public String name;
public Human(String name)
{
this.name = name;
}
}

If you put all this in one file, it would be named Main.java because the public class is called main.

import a java class from a java file which contains multiple classes

I dont want to make multiple java file for each class.

You have to if you want to access them from other packages. In Java, every file must have exactly one public class. That is a rule. There could be valid reasons why you don't want a separate file for each class, but Java doesn't care about that.

You could try making each of them static inner classes in a single outer class, and then using import static, but that is a bad practice, as you would be abusing the intended purpose of inner classes, so I don't encourage using that method. But you could.

multiple classes in a single physical file on android

You could create your custom objects as public inner classes of a CustomObjects class:

public class CustomObjects {
public class Region {
public int RegionID;
public String Name;
}
public class System {
public int SystemID;
public String Name;
}
}

But you couldn't use static members in the inner classes unless they were static themselves.



Related Topics



Leave a reply



Submit