-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 ");
    }
}
3
  • Show some documentation or something that makes you think that this isn't possible. Commented Jul 31, 2014 at 14:26
  • 3
    possible duplicate of File name and class name different in java Commented Jul 31, 2014 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. Commented Jul 31, 2014 at 14:32

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

You didn't get any downvotes, actually. (Edit, apparently I'm just late.)
@Unihedron - there were at first; I was surprised as well
@Unihedron I did get a few, but apparently they backed off. It's not a perfect answer to not a perfect question... but it tells about the problem.
@EelLee You marked it as duplicate and answered it. Why ?
@sᴜʀᴇsʜᴀᴛᴛᴀ I answered it first, then I thought it might be a duplicate question. BTW, is there anything wrong to answer in this case?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.