Can source file contain more than one class declaration

Can source file contain more than one class declaration ?

Answer : Of course yes. But there could be only one public class can have per .java file because public classes should be the same name as in the source file.

There is no limiit as much we need it. Basic purpose to include multiple java classes in one source file have to keep together all related functionality. But it is advisable not to define too many classes in one public class because it could effect readability of the code when somebody start maintenance work.

Please see example below:

package com.javahonk.multipleTest;

public class MultipleFileTest {

	public static void main(String args[]) {
		Test.testMethod();
	}

}

class Test {
	static void testMethod() {
		System.out.println("Multiple class test");
	}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *