Can a Java File Have More Than One Class

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.

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 :)

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.

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 one java class/file be part of more than one package?

Technically you can have a same class with same content in two different packages but then when you use these 2 classes in another Java class then you would have to be very specific (absolute package name) when using either of the class.

Let me give an example ...

Here is class Testing which has exactly same members but are defined in two different packages i.e. com.overflow.stack and com.stack.overflow.

When they are used in an another class Test, you have to import both of them and use absolute package name for at least one of the Testing class so that Java compiler understand which instance is which class (alternatively you can use absolute package name for both the Testing class instances).

--

package com.overflow.stack;
public class Testing {
public void whoAmI() {
System.out.println(this.getClass().getCanonicalName());
}
}

--

package com.stack.overflow;
public class Testing {
public void whoAmI() {
System.out.println(this.getClass().getCanonicalName());
}
}

--

package com.stackoverflow;

import com.overflow.stack.Testing;

public class Test {

public static void main(String[] args) {
// not using absolute package name
Testing test1 = new Testing();
test1.whoAmI();

// must use absolute package name if want to use Testing class
// from different package then above.
com.stack.overflow.Testing test2 = new com.stack.overflow.Testing();
test2.whoAmI();
}
}

Sample Run:

com.overflow.stack.Testing
com.stack.overflow.Testing

That being said, if you or your team or organization is the author of this class then you should avoid having copies of classes in different packages as it will lead to redundant code duplication and will be very confusing for the consumers of these classes. Also it is highly possible that these copies will get out of sync and possibly lead to RuntimeException which are difficult to debug and can crash the application.

Why can't two public classes be defined in one file in java?

As per java language specification, there can be only one public class in a file (.java) and file name should be same as public class name.

If you want class B accessible in other placs, you may create a separate B.java file and move your Class B code to that file.

This thread may give you some more information.

Is it a bad practice to have multiple classes in the same file?

I think you should try to keep your code to 1 class per file.

I suggest this because it will be easier to find your class later. Also, it will work better with your source control system (if a file changes, then you know that a particular class has changed).

The only time I think it's correct to use more than one class per file is when you are using internal classes... but internal classes are inside another class, and thus can be left inside the same file. The inner classes roles are strongly related to the outer classes, so placing them in the same file is fine.



Related Topics



Leave a reply



Submit