• Register
0 votes
52 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,050 points

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,930 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 13 views
13 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 Daniel Aguilar
3 votes
1 answer 836 views
836 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 Gavin 15.3k points
0 votes
1 answer 38 views
38 views
Problem : In Java, I want to have something as below : class MyClazz<T> {  static void doIt(T object) {    // More code  } } But I face below error &ldquo;Cannot make a static reference to the non-static type T&rdquo; I am unable to understand ... about a subject. Could someone clarify if such the use is possible, by the similar manner? Also, why was my original attempt went unsuccessful?
asked Jan 21 jwilliam 3.9k points
0 votes
1 answer 35 views
35 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 maddi86 5.4k points
0 votes
1 answer 32 views
32 views
Problem: In order to redirect a file to the standard input of a command, what metacharacter should be used?
asked Apr 6 ArifulIslam 5.7k points