Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

eclipse

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?

like image 967
Mistu4u Avatar asked Nov 06 '13 14:11

Mistu4u


People also ask

Can not be resolved to a type java?

This means that your project isn't setup to include the JUnit libraries when it compiles; JUnit is not included in the Java runtime libraries (JRE System Library) so you have to add it to the build path.


2 Answers

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();       }   } 
like image 140
Raju Sidda Avatar answered Oct 06 '22 03:10

Raju Sidda


Cant have two public classes in same file

   public class StaticDemo{ 

Change to

   class StaticDemo{ 
like image 29
Paul Samsotha Avatar answered Oct 06 '22 02:10

Paul Samsotha