• Register
0 votes
128 views

Problem:

When the class name and the filename of a given Java program doesn’t match I get this error. Considering an example where file name is bal.java:

public class Bal {

 public static void main(String[] args) 
{
            System.out.println("Hello, chutmarani!");
        }


}

1 error found :

File:bal.java [line 1]

Error: class Bal is public, should be declared in a file named bal.java

Can anyone help?

 

10 7 2
6,060 points

Please log in or register to answer this question.

3 Answers

0 votes

Answer:

Since bal does not match with Bal, the code will not compile properly.
To fix this error kind of error, either we need to rename the file or change the class name as “bal.java”As the error message suggests, if we declare a class as public, It needs its bal.java file. If we don’t want to do that, we don’t define it as public.

11 6 4
34,950 points
That class name and Sysout string! Lol
0 votes

Solution:

Name of public class should match the name of .java file in which it is placed (like public class Foo{} should 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 {

The name of the public class inside a file has to be the similar as the name of that file.

Therefore in case your file declares class WeatherArray, it requires to be named WeatherArray.java

For example, I renamed my MainActivity class to MainnActivity just (!) in the code. I obtain this error instantly.

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

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

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

Your file is called Main.java where it must be

WeatherArray.java

A Java file can just have 1 public class in it. The name of that public class should be the similar as the filename.

To the file MyClass.java must comprise a class MyClass that may or may not be public. However it should not comprise a public class with any other name.

10 6 4
31,120 points
0 votes

Solution:

Name of public class should match the name of .java file in which it is placed (like public class Foo{} should 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 {

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

Therefore in case your file declares class WeatherArray, it requires to be named WeatherArray.java

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

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

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

Your file is called Main.java where it must be

WeatherArray.java

I had the similar problem however solved it at the time I convinced that I didn't compile it with the appropriate casing. You may have been doing

A Java file can just have 1 public class in it. The name of that public class should be the similar as the filename.

To the file MyClass.java must comprise a class MyClass that may or may not be public. however it should not comprise a public class with any other name.

From my comprehension for same question, the name of the file must be the similar as the public class, and at the time i alter the class name to R_2 (i refactored the filename to R_2.java from R 2.java), it throws this error.

error: invalid method declaration; return type required

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 72 views
72 views
How can I fix this?
asked Dec 27, 2020 TeamScript 13.5k points
0 votes
1 answer 42 views
42 views
Problem: class is public should be declared in a file named Main.java:1: error: class WeatherArray is public, should be declared in a file named WeatherArray.java public class WeatherArray { ^ 1 error
asked Feb 23 Hira 2.2k points
0 votes
1 answer 40 views
40 views
I want a proper solution to remove this warning on sonar.
asked Jan 19 TeamScript 13.5k points
0 votes
1 answer 17 views
17 views
java file with a default class access specifier get compiled?
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 1 view
1 view
Problem: Need your help to solve this >'when should base class members be declared protected?' ?
asked Mar 20 Samiur1 38.4k points
0 votes
1 answer 158 views
158 views
Problem: Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice: class Institutions { public void print_institutions() { System.out.println("This is an institution"); } } class ... , I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!
asked Dec 29, 2020 Code Learner 9.2k points
0 votes
1 answer 37 views
37 views
Hi, when i try to execute this code i always get the error message from above, and i really want to know why, already tried to put a “public class XXXX” before the code, but not work anyway. public static String invertirPalabras(String cadena) { cadena += " "; String ... + palabra; if (cadena.charAt(i) == ' ') { nuevaCadena += palabra; palabra = ""; } } return nuevaCadena.trim(); }
asked Aug 2, 2020 Daniel Aguilar
3 votes
1 answer 1.9K views
1.9K views
Problem: Hello Kodlogs, I am a novice Java programmer. I just wrote my very first program a few minutes earlier. Now I want to write a program that can add two different integer numbers for me. I guess I done my coding well but it says something wrong with my writing and throws back an error: &ldquo ... []args){ int x = 10; int y = 15; int sum = x+y; System.out.println("Sum is: " + sum); } }
asked Mar 23, 2020 Gavin 15.3k points
0 votes
1 answer 119 views
119 views
Problem: Which of the following will open a file named MyFile.txt and allow you to read data from it? a. File file = new File("MyFile.txt"); b. Scanner inputFile = new Scanner("MyFile.txt"); c. File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); d. PrintWriter inputFile = new PrintWriter("MyFile.txt"); Does any one know correct file name?
asked Feb 22, 2020 maddi86 5.4k points
0 votes
1 answer 6 views
6 views
Problem: Can anyone tell me the solution of this problem? .. Enumerations (enums) must be declared in their own class. true or false
asked Apr 1 Ifra 24.4k points