Chris Frank
Chris Frank

Reputation: 4442

Error: class X is public should be declared in a file named X.java

I am trying to write a program, but I'm getting this compiler error:

Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java
public class WeatherArray {
       ^
1 error

I have checked my file names, and my public class is the same as my .java file.

How can I fix this?

Here is my code:

public class WeatherArray {
    public static void main(String[] args) {
        // ...
    }
}

Upvotes: 75

Views: 627189

Answers (23)

Fatima Mahmood
Fatima Mahmood

Reputation: 1

public class Main {
    public static void main(String[] args) {
        Employee employee = new Employee(1, "John Doe", "Software Engineer", 50000.0, "1234567890");

        employee.updateName("John Doe Jr.");
        employee.updatePosition("Senior Software Engineer");

        // Attempt to update salary without authorization
        employee.updateSalary(60000.0, "INVALID_CODE"); // Unauthorized access denied!

        // Update salary with authorization
        employee.updateSalary(60000.0, "AUTHORIZED");

        // Retrieve and display employee details
        System.out.println("Employee ID: " + employee.getEmployeeID());
        System.out.println("Name: " + employee.getName());
        System.out.println("Position: " + employee.getPosition());
        System.out.println("Salary: " + employee.getSalary());
        System.out.println("Bank Account Number: " + employee.getBankAccountNumber());
    }
}

java: class Main is public, should be declared in a file named Main.java.solve thsi error in the above code

Upvotes: 0

Cool_Coder
Cool_Coder

Reputation: 5073

This solution is based on my experience with Kotlin, but probably should also work for Java.

In my resource files I had 2 files : item_alert_edit.xml and item_alertedit.xml. The compiler converts these files to .java and for some reason removes the _, so effectively both files will have the same name. I solved it by renaming one of the files to be different and this solved the error for me. Clean build, etc did not work for me.

Upvotes: 0

Rohaitas Tanoli
Rohaitas Tanoli

Reputation: 797

Just experience this issue today. Thanks to viewBinding.

Problem was the autoGenerated binding. I had two xml files

  • one_two.xml
  • onetwo.xml

Solution # 1 : Just use

tools:viewBindingIgnore="true"

in any one file.

Solution # 2 :

Rename the xml file

Upvotes: 2

BinCodinLong
BinCodinLong

Reputation: 721

Check to make sure you don't have two identical -- or very nearly identical -- files in your res/layout folder.

Upvotes: 0

Yuvraj Singh
Yuvraj Singh

Reputation: 69

Yes! When you face these type of problem then try to following points

  • Check Your .java file name and class name.
  • If Class name and public class name are not the same then RENAME class name.

Upvotes: 1

answer42
answer42

Reputation: 73

If your class name is the same as the filename then check that it does not contain any zero width character

I accidentally copied a class name with invisible whitespace which caused this exception

Eclipse was able to build the file and Gradle was not

This can be very confusing

Upvotes: 0

iamfnizami
iamfnizami

Reputation: 183

Compile WeatherArray.java instead of Main.java

This error comes if you have not saved your source code with the same name of your public class name.

Upvotes: 0

Sachin Singh
Sachin Singh

Reputation: 1

If you make WeatherArray class from public to default.

public class WeatherArray =====> class WeatherArray

then you do not get any error and you can easily compile your code by just writing

==> javac any_name_you_assign_to_file.java

Now a WeatherArray.class will generate.

To run your code you have to use class name

==> java WeatherArray

Upvotes: 0

NotTooTechy
NotTooTechy

Reputation: 486

From Ubuntu command line:

//WeatherArray.java
public class WeatherArray {
  public static void main(String[] args) {
    System.out.println("....Hello World");
}}

ls

WeatherArray.java

javac WeatherArray.java

ls

WeatherArray.java WeatherArray.class

java WeatherArray

....Hello World

Of course if you name your java file with different name than WeatherArray, you need to take out public and it would be:

// Sunny.java
class WeatherArray {
   public static void main(String[] args) {
      System.out.println("....Hello World"); }}
// javac Sunny.java; java WeatherArray

Upvotes: 0

jlordo
jlordo

Reputation: 37813

Name of public class must match the name of .java file in which it is placed (like public class Foo{} must be placed in Foo.java file). So either:

  • rename your file from Main.java to WeatherArray.java
  • rename the class from public class WeatherArray { to public class Main {

Upvotes: 106

Warren Feeney
Warren Feeney

Reputation: 57

I had the same problem but solved it when I realized that I didn't compile it with the correct casing. You may have been doing

javac Weatherarray.java

when it should have been

javac WeatherArray.java

Upvotes: 5

Dylan
Dylan

Reputation: 399

The terminal is not case sensitive when writing "Javac [x].java", so make sure what you write in the terminal matches the filename and class name.

My class name and file name were both "MainClass", but I was compiling using "Mainclass". Notice I forgot to make the "c" capital.

Upvotes: 0

Ng Sek Long
Ng Sek Long

Reputation: 4786

In my case (using IntelliJ) I copy and pasted and renamed the workspace, and I am still using the old path to compile the new project.

In this case this particular error will happen too, if you have the same error you can check if you have done the similar things.

Upvotes: 0

Yash P Shah
Yash P Shah

Reputation: 809

I encountered the same error once. It was really funny. I had created a backup of the .java file with different filename but the same class name. And kept on trying to build it till I checked all the files in my folder.

Upvotes: 0

Rahat Batool
Rahat Batool

Reputation: 17

To avoid this error you should follow the following steps:

1) You should make a new java class

You should make a new java class.

2) Name that class

Name that class

3) And a new java class is made

And a new java class is made

Upvotes: 0

abc123
abc123

Reputation: 587

I my case, I was using syncthing. It created a duplicate that I was not aware of and my compilation was failing.

Upvotes: 0

Sangoo
Sangoo

Reputation: 1

when you named your file WeatherArray.java,maybe you have another file on hard disk ,so you can rename WeatherArray.java as ReWeatherArray.java, then rename ReWeatherArray.java as WeatherArray.java. it will be ok.

Upvotes: -1

dhiraja gunjal
dhiraja gunjal

Reputation: 1

error example:

public class MaainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

  }
}

correct example:just make sure that you written correct name of activity that is"main activity"

public class MainActivity extends Activity {


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

  }
}

Upvotes: -1

sandalone
sandalone

Reputation: 41749

This happens when you have 1 name for the Java class on hard disk and another name of Java class in the code!!

For example, I renamed my MainActivity class to MainnActivity only (!) in the code. I got this error immediately.

There is also a visual indicator in the Project tab of Android Studio - a class inside a class, like you have nested classed, but with an error indicator.

The solution is to simply rename class name in the Project tab (SHIFT + F6) to match the name in the Java code.

Upvotes: 5

Rohan Kumar
Rohan Kumar

Reputation: 1

The answer is quite simple. It lies in your admin rights. before compiling your java code you need to open the command prompt with run as administrator. then compile your code. no need to change anything in your code. the name of the class need to be the same as the name of the java file.. that's it!!

Upvotes: -1

AlexWien
AlexWien

Reputation: 28727

your file is named Main.java where it should be

WeatherArray.java

Upvotes: 1

PermGenError
PermGenError

Reputation: 46408

You named your file as Main.java. name your file as WeatherArray.java and compile.

Upvotes: 2

BostonJohn
BostonJohn

Reputation: 2661

The name of the public class within a file has to be the same as the name of that file.

So if your file declares class WeatherArray, it needs to be named WeatherArray.java

Upvotes: 15

Related Questions