• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why should a java file not contain more than one public class?

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, after 3 years of learning java and involving in few projects, I got this doubt today. Why cant we write more than one public class in a single java file?

Thank you all in advance.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Perhaps below links might help you:
https://coderanch.com/t/404122/java/java/why-public-class
https://coderanch.com/t/474617/java-programmer-SCJP/certification/Why-there-has-one-only
https://coderanch.com/t/397380/java/java/One-Public-Class-Per-Source
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:Hi,

Perhaps below links might help you:
https://coderanch.com/t/404122/java/java/why-public-class
https://coderanch.com/t/474617/java-programmer-SCJP/certification/Why-there-has-one-only
https://coderanch.com/t/397380/java/java/One-Public-Class-Per-Source


I have gone through the links. But still I am not clear. So much confusing. Let me explain my doubt clearly.

If there are two public classes the compiler generates an exception. Why? The compiler itself is a very complex program to write. Why dint java developers code the compiler like this, parse all the classes in the file given to the compiler and check which class has the main method.

In few threads I found that, this rule makes the compilation a bit fast. Can anyone explain how it is going to effect the performance of the compiler?

Now, if restricting to have only one public class improves the performance of the compiler, what if I don't have any public class at all. Now how is the performance improved?

If performance is just a story, then can I assume that this is just a rule.

1. You cannot have 2 public classes in one unit. (This is just a rule imposed without any proper reason)
2. The file name should be named with the class name if it is declared public. (This is a dumb rule imposed I assume if the above assumptions are true)
3. If class is not declared public it can be named with any name. (I guess there is no reason why compiler and interpreter are writing like this)

If my assumptions are wrong, kindly tell me the answer how performance is increased if it has only one public class. Other question is what about the performance when there is no public class in a single java file that has more than one class?

Thank you all in advance. Good day.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I very much doubt it's anything to do with performance. To be sure about the reason you'd have to ask the language designers, but my guess is that they decided it would be good practice to have a separate file per public class, named after the class, to make it easy to manage your classes. So they decided to enforce that.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:I very much doubt it's anything to do with performance.


Are you saying it is something related to performance or it has nothing to do with performance.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:

Matthew Brown wrote:I very much doubt it's anything to do with performance.


Are you saying it is something related to performance or it has nothing to do with performance.


I'm saying that I don't think it's anything to do with performance. Simplicity, maybe.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm..Thank you Mr. Brown. So they are just rules imposed on developers. The compiler is designed in that way only with no proper explanation from the language designers.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to say, I think it's a perfectly sensible decision.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having one class per file makes your classes easier to find and read IMO.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Having one class per file makes your classes easier to find and read IMO.

I want to know how it going to make it easy? Can you please tell me Mr. Boswell
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:

James Boswell wrote:Having one class per file makes your classes easier to find and read IMO.

I want to know how it going to make it easy? Can you please tell me Mr. Boswell



If you want to find a public class named some.package.AnImportantClass you know exactly where to find the code for that class: src/some/package/AnImportanClass.java. If you were able to have more then one public class per file, and if you were allowed to name the file whatever you wanted (rather than being forced to name the file after the one public class), then that class could be in any .java file in the right package. You would have to look through them all. Worse: you could write one big .java file which contained all the classes and you would have to look through tons of lines of code to find where AnImportantClass is.

The current mechanism promotes class-independence, makes it easier for us (the humans) to find the class (and as was previously said, may simplify the JVM code required to find and load the intended class).
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:

James Boswell wrote:Having one class per file makes your classes easier to find and read IMO.

I want to know how it going to make it easy? Can you please tell me Mr. Boswell


Want to know where a particular public class is? It's in the file of the same name. There, easy.

When programming with C# I use exactly the same convention (and the IDE gives me functionality that supports it) even though the language doesn't have the same constraints. It's a sensible arrangement. It might not be the only sensible arrangement, but you only need one!
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:

chaitanya karthikk wrote:

James Boswell wrote:Having one class per file makes your classes easier to find and read IMO.

I want to know how it going to make it easy? Can you please tell me Mr. Boswell



If you want to find a public class named some.package.AnImportantClass you know exactly where to find the code for that class: src/some/package/AnImportanClass.java. If you were able to have more then one public class per file, and if you were allowed to name the file whatever you wanted (rather than being forced to name the file after the one public class), then that class could be in any .java file in the right package. You would have to look through them all. Worse: you could write one big .java file which contained all the classes and you would have to look through tons of lines of code to find where AnImportantClass is.

The current mechanism promotes class-independence, makes it easier for us (the humans) to find the class (and as was previously said, may simplify the JVM code required to find and load the intended class).


Okay this makes sense. Suppose if I don't mention a public class, I have an option to save it with any name. What about easy in this case? Will the compiler search through the tons of lines of code?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:Okay this makes sense. Suppose if I don't mention a public class, I have an option to save it with any name. What about easy in this case? Will the compiler search through the tons of lines of code?


Possibly, but what do you care? It's only run once (hopefully ).

Speculation of this sort seems kind of pointless, unless you're actually trying to work out how you might fool the compiler. The rules are simple; follow 'em. If you don't like 'em, there are tons of other languages out there.

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
Speculation of this sort seems kind of pointless, unless you're actually trying to work out how you might fool the compiler. The rules are simple; follow 'em. If you don't like 'em, there are tons of other languages out there.

Winston

No I love java. I just wanted to know what are the reasons behind such kind of rules. If someone asks me the same question I don't want to put a dumb face.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, so finally I believe we can conclude that

1. Forcing the file saved with the class name will increase the performance while looking for files/classes.
2. However if class is not declared public the compiler/interpreter has to search for the entry point. So it is better to declare all classes as public in their respective files.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:2. However if class is not declared public the compiler/interpreter has to search for the entry point. So it is better to declare all classes as public in their respective files.



No, don't do that. Compiler performance really isn't very important, and is much less important that good design. Classes should be public or not public according to their requirements.
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Classes should be public or not public according to their requirements.

Okay at least the main entry point class must be declared public. Is this okay now?
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Classes should be public or not public according to their requirements.

So why would I want to declare a class not public. What difference is i going to make? In other words in what situations one would want to create a not public class? I could not understand this. Can you please explain?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a top-level class has default access (it's not public), then it is only visible to classes inside the same package. Classes outside the package don't know that the class exists.

One of the principles of object oriented design is that you hide as much implementation details as possible. A component in your program or library should only expose to the outside that what's strictly necessary to expose. That way you can keep the API of the component small and simple. In particular, a component should not show any details of its implementation to the outside world. By hiding implementation details, it's possible to change the implementation later without the users of the component having to know about it.

Making a class non-public is one way to hide it from the outside of a component.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to what Jesper said, it's exactly the same principle as why you should make instance variables private, but on a package basis rather than a class basis. If there are any classes that don't need to be visible outside the package, don't make them visible. That way you're free to change them later while knowing you can't affect any code using your package. Anything that's public can't be changed without there being potential consequences.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:So why would I want to declare a class not public. What difference is i going to make? In other words in what situations one would want to create a not public class? I could not understand this. Can you please explain?


What Jesper said. In fact, you should be looking for a reason to make a class public. If you can't find one, don't do it; but don't give its source file a different name, just to prove that you can make the compiler work hard.

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks all of you. Now that I understood. Thanks a ton. Love java, love coderanch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic