"The public type <<classname>> must be defined in its own file" error in Eclipse [duplicate]

I have written the following code:

package staticshow;


public class StaticDemo {
  static int a = 3;
  static int b = 4;

  static {
    System.out.println("Voila! Static block put into action");
  }

  static void show() {
    System.out.println("a= " + a);
    System.out.println("b= " + b);
  }
}

public class StaticDemoShow {
  public static void main() {
    StaticDemo.show(); 
  }
}

I am getting the error message:

The public type StaticDemo must be defined in its own file

error in the very first line public class StaticDemo {. Why is it happening and how can I resolve it? Note that my project name is StaticDemoShow, package name is staticshow and class names are as given in the code.

EDIT- After making just one class public or both the classes default, I am getting the error "Selection does not contain a main type". Now what should I do?


Solution 1:

If .java file contains top level (not nested) public class, it has to have the same name as that public class. So if you have class like public class A{...} it needs to be placed in A.java file. Because of that we can't have two public classes in one .java file.

If having two public classes would be allowed then, and lets say aside from public A class file would also contain public class B{} it would require from A.java file to be also named as B.java but files can't have two (or more) names (at least in all systems on which Java can be run).

So assuming your code is placed in StaticDemoShow.java file you have two options:

  1. If you want to have other class in same file make them non-public (lack of visibility modifier will represent default/package-private visibility)

     class StaticDemo { // It can no longer public
    
         static int a = 3;
         static int b = 4;
    
         static {
             System.out.println("Voila! Static block put into action");
         }
    
         static void show() {
             System.out.println("a= " + a);
             System.out.println("b= " + b);
         }
    
     }
    
     public class StaticDemoShow { // Only one top level public class in same .java file
         public static void main() {
             StaticDemo.show();
         }
     }
    
  2. Move all public classes to their own .java files. So in your case you would need to split it into two files:

    • StaticDemo.java

        public class StaticDemo { // Note: same name as name of file
      
            static int a = 3;
            static int b = 4;
      
            static {
                System.out.println("Voila! Static block put into action");
            }
      
            static void show() {
                System.out.println("a= " + a);
                System.out.println("b= " + b);
            }
      
        }
      
    • StaticDemoShow.java

        public class StaticDemoShow { 
            public static void main() {
                StaticDemo.show();
            }
        }
      

Solution 2:

Cant have two public classes in same file

   public class StaticDemo{

Change to

   class StaticDemo{

Solution 3:

Java rule : One public class in one file.