7

we can declare only one public file in a source file and file name must match the public class name

is there any reason to this restriction....

saravanan
  • 5,339
  • 7
  • 45
  • 52

6 Answers6

9

Well, it's not a compulsory restriction in Java. It's one that the Java Language Specification mentions as an option. From section 7.6 of the JLS:

When packages are stored in a file system (§7.2.1), 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 compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

But basically it's there to encourage you to make your source easier to navigate. If you know the name of a public class, it's usually pretty easy to find the source code for it.

Andy Brown
  • 18,961
  • 3
  • 52
  • 62
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    It is an optional restriction, but (AFAIK) all mainstream Java implementations (compilers, JVMs, IDEs, etc) enforce it. – Stephen C Jan 28 '11 at 09:42
  • ok i understand that this rule is for convention to navigate the source file and class easily.but why i can't have more than one public file and why the same convention is not applied for non public class(default-same package) – saravanan Jan 28 '11 at 09:56
  • 2
    @user587072: What do you mean by "why I can't have more than one public file"? A file isn't public, a class is. And if you have more than one public class, they can't *both* have the relevant name - so at least one of them would be harder to find. As for why it doesn't apply to non-public classes: presumably the point is that developers are more likely to want to find the source code for a public class than a non-public one. But I'm really just guessing, to be honest. I think the convention makes sense for all classes where possible. – Jon Skeet Jan 28 '11 at 10:18
  • @Jon Skeet: As for why it doesn't apply to non-public classes..i think developers don't work on the same package provided by others so they don't need to konw the non-public(package access) classes – saravanan Jan 28 '11 at 10:27
  • @saravanan: That's pretty much what I wrote, isn't it? "developers are more likely to want to find the source code for a public class than a non-public one" – Jon Skeet Jan 28 '11 at 10:34
  • 1
    Fascinating fact. I didn't even know that the JLS specified optional features in such a way and that it would apply to such a basic java limitation. Too bad I can only give +1 (Personally I think the limitation isn't that useful in this day and age anymore.. IDEs don't care about filenames when searching classes/methods/whatever and it leads to some ugly 5liner class files) – Voo Jan 30 '12 at 02:27
1

Yes, it's the specification of the Java language...

ykatchou
  • 3,667
  • 1
  • 22
  • 27
1

The reason is, that this is the convention. Also the classloader expects a class in a specific file. You can write your own classloader to avoid this restriction, but there is no good reason to do this. Everyone looking on your code will get confused. ;)

However, you can create "multiple" classes in one file by creating inner classes. I know, its not the same, but usually you should think about more important things than why there is only one class in one file.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
0

simply remember only that class would be public which has the main other files dont be public

0

It's there so that the compiler can find the source code of dependent classes.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

@saravanan. I have executed some simple programs which show that just a single default class(ie a class with no access specifier) having the main method works well in java when u save the file name with the default class name.

To add on to the reason of naming the file with public class name, in a document I went through the details into this state that the JVM looks for the public class (since no restrictions and can be accessible from any where) and also looks for public static void main() in the public class .

This public class acts as the initial class from where the JVM instance for the java application( program) is begun.So when u provide more than one public class in a program the compiler itself stops you by throwing an error.

This is because later you can't confuse the JVM as to which class to be its initial class because only one public class with the public static void main(String args[]) is the initial class for JVM.

HOPE I have helped you in understanding JAVA programming naming better.

David Bejar
  • 512
  • 4
  • 20
DIVYA
  • 1