-6
class Blog{
    public static void main(String args[]){
        System.out.println("overflow");
    }
}

I am saving this file with name First.java and compiling it than it is generating file with Blog.class and gives output:

overflow

If same program i am writing as given below:-

public class Blog{
    public static void main(String args[]){
        System.out.println("overflow");
    }
}

it gives error after compiling

   First.java:3: error: class Blog is public, should be declared in a file named Blog.java
Omaha
  • 2,262
  • 15
  • 18
  • 7
    The error is pretty clear - save the class in a file named Blog.java – Nir Alfasi Sep 18 '13 at 17:49
  • Yeah - don't run with scissors, and don't save your Java class in a files with the wrong name... – Brad Peabody Sep 18 '13 at 17:54
  • Why does this question has so many downvotes? Are people so unforgiving of beginner errors that the OP should be punished for asking such a simple question? Shame on you. – victorantunes Sep 18 '13 at 17:58
  • @victorantunes it's because the amount of this questions about the same question here on SO. This question shows no effort searching for the error or reading the error message at all. – Martin Seeler Sep 18 '13 at 18:11

5 Answers5

1

If you declare a class as public then the file name and the class name should same.Otherwise you will get compile time error.
So change you file name to Blog.java from First.java.

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
  • i know but what is the reason if i am not declaring class as public code runs succesfully on the other way i am declaring class as public it gives error when i compile the file name as First.java – user2693384 Sep 18 '13 at 18:08
  • @user2693384 because declaring `public` is nothing but giving access to class from code in other packages. – Prabhaker A Sep 18 '13 at 18:14
  • @user2693384 please read the SO question http://stackoverflow.com/questions/2134784/why-filename-in-java-should-be-same-as-class-name – Prabhaker A Sep 18 '13 at 18:15
0

The reported error is really clear:

class Blog is public, should be declared in a file named Blog.java

Frederic Close
  • 9,389
  • 6
  • 56
  • 67
  • i know but what is the reason if i am not declaring class as public code runs succesfully on the other way i am declaring class as public it gives error when i compile the file name as First.java – user2693384 Sep 18 '13 at 18:10
0

java classes always should have same name with its file name (case sensitive) with .java extension. so you should save it Blog.java

Saeed Alizadeh
  • 1,417
  • 13
  • 23
  • If I am not wrong you can save with different file name than your class name provided that class is not public. – Smit Sep 18 '13 at 17:57
  • i know but what is the reason if i am not declaring class as public code runs succesfully on the other way i am declaring class as public it gives error when i compile the file name as First.java – user2693384 Sep 18 '13 at 18:09
0

Because it is part of the Java Language Specification! You may also have a look at Managing Source and Class Files in the Java Tutorials.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94
  • i know but what is the reason if i am not declaring class as public code runs succesfully on the other way i am declaring class as public it gives error when i compile the file name as First.java – user2693384 Sep 18 '13 at 18:10
0

From the JLS

If and only if packages are stored in a file system (§7.2), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:

•The type is referred to by code in other compilation units of the package in which the type is declared.

•The type is declared public (and therefore is potentially accessible from code in other packages).

This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a Java compiler to find a named class within a package. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.

GriffeyDog
  • 8,186
  • 3
  • 22
  • 34