wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why should a java file not contain more than one public class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why should a java file not contain more than one public class?" Watch "Why should a java file not contain more than one public class?" New topic
Author

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

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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.


Love all, trust a few, do wrong to none.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

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


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806


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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Sep 15, 2009
Posts: 806

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

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Sep 15, 2009
Posts: 806

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

Joined: Apr 06, 2010
Posts: 4461
    
    8

I have to say, I think it's a perfectly sensible decision.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Having one class per file makes your classes easier to find and read IMO.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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).


Steve
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Sep 15, 2009
Posts: 806

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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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

Joined: Sep 15, 2009
Posts: 806

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

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Sep 15, 2009
Posts: 806

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

Joined: Sep 15, 2009
Posts: 806

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?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4461
    
    8

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

Joined: Mar 17, 2011
Posts: 8196
    
  23

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

Joined: Sep 15, 2009
Posts: 806

Many thanks all of you. Now that I understood. Thanks a ton. Love java, love coderanch.
 
 
subject: Why should a java file not contain more than one public class?