• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why classes are splitted in multiple files?  RSS feed

 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I know this is an absurd question but i've this doubt. Why when code is developed, sometimes classes etc. are splitted in different files, other times instead all classes etc. are in one single file?

Thanks

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of the time, you declare a single Java class per file. Sometimes a class is an inner class. For example, maybe it is only used from one class. Or it is a tiny class like a Comparator that isn't going to be reused.

At the beginning and whenever you aren't sure, you won't go wrong with the "one class per file" rule.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for reply! But there is a specific reason for all this? Or more? :P
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a convention used in java where class name is same as file name by which we save the file
so if you will have multiple class in a single file you wont be sure which class name you should have as your file name

if that what you asked
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler usually enforces that convention if you have a public class.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would I want to have all my code cramped together in a single file? what a mess!
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:there is a convention used in java where class name is same as file name by which we save the file
so if you will have multiple class in a single file you wont be sure which class name you should have as your file name

if that what you asked


This isn't related to the main class only?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:This isn't related to the main class only?


No, that rule isn't limited to classes which happen to have a public static void main(String[]) method. (Don't refer to that as "the" main class.) It applies to all public classes, and the compiler enforces that rule because when it compiles class A, and when it finds a reference to class B in that code, it needs the compiled version of class B to evaluate whether class A used it correctly. If it can't find the compiled version of class B then it will look for the source code for class B and compile it -- and to do that it needs to know the name of the file which contains that source.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the details

But there is some controindication for using a single file instead of different for each class?
If i have classes A and B in a single file, and i rename the file with the 'public static void main' class name, it'll work right?
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on what you mean by "work". We've already pointed out that public classes have to be in a file whose name matches the class name. And also that the presence or absence of a public static void main(String[]) method is irrelevant to that rule. So it's now unclear what your question is.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm interested to know why this happens, why this convention exists?

What are the benefits to utilize a file for each class ?
And the disadvantages to use one only?

I seem to remember that in the past with a particuar code, i don't remember what, the compiler asks me to use different files.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:I'm interested to know why this happens, why this convention exists?


As I already said: If [the compiler] can't find the compiled version of class B then it will look for the source code for class B and compile it -- and to do that it needs to know the name of the file which contains that source. (Which would be B.java of course.)

What are the benefits to utilize a file for each class ?


If the programmer wants to find the source code for class X, they know exactly where to look for it. No need for goofy naming standards or firing up the "search" tool in your IDE.

And the disadvantages to use one only?


The disadvantages of using one file, only, to store the source code for all classes? Or the disadvantages of using one file to store one class only?
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The disadvantage of using one file for all classes.

But if i use two classes in a single source code file, than i put in that file the 'public static void main' method too, and i rename the file with this one class' name, the program will compile and run correctly right? This is the sense of my question.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be simpler if you just tried whatever it is that you're asking. I repeat that having a public static void main(String[]) method isn't going to affect whatever happens, because the compiler doesn't care anything about that method. It's just another static method if it's there, and if it isn't there it makes no difference to the compiler.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Addeo wrote:The disadvantage of using one file for all classes.


Well, first of all only one of the 657 classes can be public. That makes it hard for other applications to use them. And second of all it makes it hard for programmers to find the classes, since you've got several hundred classes all strung together in one enormous text file.
 
Ivan Addeo
Ranch Hand
Posts: 52
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Only one of the 657 classes can be public'.

What this means? I can't have multiple public class in a program/source code?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means if you use one file only one class can be public. If you use 657 files, then not more than 657 classes can be public.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!