-6

This file name is saved as abc.java but it can be compiled and generates xyz.class. Why is this possible?

class xyz {
    public static void main(String args[]) {
        System.out.println("hello ");
    }
}
katira
  • 1
  • 1
  • 3
  • Show some documentation or something that makes you think that this isn't possible. – takendarkk Jul 31 '14 at 14:26
  • 3
    possible duplicate of [File name and class name different in java](http://stackoverflow.com/questions/19731853/file-name-and-class-name-different-in-java) – Eel Lee Jul 31 '14 at 14:26
  • And I think this question doesn't deserve so many downvotes. I guess that's why you should post a formatted question, because if it would be in this form from the beginning, it could get some upvotes, too. – Eel Lee Jul 31 '14 at 14:32

1 Answers1

5

Because only public classes need to be named the same as the .java file. A class which isn't declared public can be kept in a file of a different name.

Edit: For a public class, javac would generate the following error:

abc.java:1: error: class xyz is public, should be declared in a file named xyz.java
public class xyz {
       ^
1 error
Eel Lee
  • 3,513
  • 2
  • 31
  • 49