Handling Java class with more than one class

In this tutorial we will see how to name, compile and run a java program when there is more than one class in a java file.

Example 1 : Consider the following program.

class ClassOne
{
     public static void main(String[] args)
     {
         ClassTwo.methodOfClassTwo();
     }
}
 
class ClassTwo
{
     static void methodOfClassTwo()
     {
         System.out.println("From Class Two");
     }
}

Naming

You can save this java program with any name. It can be ClassOne.java or it can be ClassTwo.java or it can be anything.java.

Compile

You should compile your java program with the name which is given to class name like >javac ClassOne.java or >javac ClassTwo.java or >javac anything.java.

When you compile a java file, the number of .class files generated will be equal to number of class definitions in it. i.e If a java file has one class definition, one .class file will be generated. If it has two class definitions, two .class file will be generated and so on.

Running

Now, compiling the above program there will be two class file generated. Then which one to run? is it >java ClassOne or is it >java ClassTwo    It must be >java ClassOne, because execution of any java program start with main() method. If you try to run >java ClassTwo, you will get an error: Main method not found in class ClassTwo, please define the main method as public static void main(String[] args).

Example 2: Now consider same example with litte modification, just declare ClassOne as public.

public class ClassOne
{
     public static void main(String[] args)
     {
          ClassTwo.methodOfClassTwo();
     }
}
 
class ClassTwo
{
     static void methodOfClassTwo()
     {
          System.out.println("From Class Two");
     }
}

Naming

Here, you must name your file as “ClassOne.java”. You can’t give any other name. If you give any other name you will get compile time error : class ClassOne is public, should be declared in a file named ClassOne.java.

Compile

As only one name is allowed in this case, you should only compile with same class name i.e >javac ClassOne.java.

Running

That must be >java ClassOne. because this is only class that has main() method.

Example: 3 Now make little more modifications to the program. Declare ClassTwo as public and ClassOne as default.

class ClassOne
{
     public static void main(String[] args)
     {
          ClassTwo.methodOfClassTwo();
     }
}
 
public class ClassTwo
{
     static void methodOfClassTwo()
     {
          System.out.println("From Class Two");
     }
}

Naming

You have to save this file with name as “ClassTwo.java”. If you give any other name you will get compile time error becuase ClassTwo is public.

Compile

It must be >javac ClassTwo.java.

Running

You must name this program as ClassTwo.java, you must compile this program as >javac ClassTwo.java but you have to run it as >java ClassOne not as >java ClassTwo. Because only ClassOne has main() method. ClassTwo doesn’t have main() method. If you run it as >java ClassTwo, you will get run time error : Main method not found in class ClassTwo, please define the main method as public static void main(String[] args).

Example: 3 Now make little more modifications to the program. Declare both the classes as public.

public class ClassOne
{
     public static void main(String[] args)
     {
          ClassTwo.methodOfClassTwo();
     }
}
 
public class ClassTwo
{
     static void methodOfClassTwo()
     {
          System.out.println("From Class Two");
     }
}

Naming

Whatever you give the name, whether it is ClassOne.java or ClassTwo.java or anything.java,  you will get compile time error. Because One java file should contain only one or zero public class. It should not contain more than one public class.

Example 5: Look at the following program.

class ClassOne
{
     public static void main(String[] args)
     {
          System.out.println("From Class One");
     }
}
 
class ClassTwo
{
     public static void main(String[] args)
     {
          System.out.println("From Class Two");
     }
}

Naming

You can save this program with any name. It can be ClassOne.java or it can be ClassTwo.java or it can be anything.java as there is no public class.

Compile

You have to compile this program with the name you have given i.e >javac ClassOne.java or >javac ClassTwo.java or >javac anything.java

Running

Notice that both the classes have main() method. You can run both the classes with their name. i.e  If you trigger >java ClassOne, you will get From Class One as output. If you trigger >java ClassTwo, you will get From Class Two as output.

Leave a Reply