-3

I am fairly new to Java, and whenever we have written programs in school, we put each class in a separate java file, but I do not remember why this was (if there was even a reason). Right now I am going back over my notes and trying to "understand" how it all works.

Is this necessary to do, and why? Moreover, is there a distinction about where the files should be stored—e.g. in the same directory?

E.g. if we have a class 'Vehicle' and a class 'Car' extending 'Vehicle', do we need to save 'Vehicle' and 'Car' in java files 'Vehicle.java' and 'Car.java', respectively, or could both classes be saved in (say) 'Test.java'?

Duplication disclaimer: my question differs from this one, as mine asks whether it is possible to put multiple classes in one .java file; I am specifically interested in the explanation, i.e. why it is/not possible. I am not interested in what the method is called (the linked question just says 'it is necessary, now what is it called?'): my question is more basic than this.

Community
  • 1
  • 1
Szmagpie
  • 192
  • 2
  • 11
  • 1
    Public classes need to be in different files – Andrew Li Jul 03 '16 at 18:59
  • 3
    Related http://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file – Tunaki Jul 03 '16 at 19:01
  • @DVers, please explain if I can edit this question to make it more appropriate for the site. I could not find an answer from searching the internet nor stackoverflow. – Szmagpie Jul 03 '16 at 19:12
  • @Tunaki thanks for the link – Szmagpie Jul 03 '16 at 19:13
  • I was going to write a full answer, but I figured this should help https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html One of the biggest issues I notice from new Java programmers is where to put classes. Do they have t be n separate classes? if you want other classes to benefit from the Vehicle class, then yes, separate, but if car is the only one working with it then you could technically put it in one class, but that might be looked at as a bad practice. – XaolingBao Jul 03 '16 at 19:55

1 Answers1

7

It's possible to place multiple classes in one .java file. However, this is a bad idea. Here's why:

package foo.bar;

public class Foo { }

public class Bar { }

A source file that contains the above code as-is won't compile, as each .java file can contain only one public class/interface/enum (btw, these are called "type"s). As a result you have to do this to keep them in the same file:

package foo.bar;

public class Foo { }

class Bar { } // Not public

Or this:

package foo.bar;

class Foo { } // Not public

public class Bar { }

Now here comes the drawback: If the non-public type(s) contain something useful to everything (such as a method for generating random numbers over a normal distribution), it won't be accessible to anything that is not in the package called foo.bar!

Besides, it's easier to manage small source files.


Moreover, is there a distinction about where the files should be stored—e.g. in the same directory

Yes - this depends on the package names. For example, the packages foo.bar, foo.baz, foo.qux, bar.foo, bar.baz and bar.qux would look like these subfolders of a common source folder (let's call it src):

./src/
L___ ./src/foo/
|    L__ ./src/foo/bar/
|    L__ ./src/foo/baz/
|    L__ ./src/foo/qux/
L___ ./src/bar/
|    L__ ./src/bar/foo/
|    L__ ./src/bar/baz/
|    L__ ./src/bar/qux/

When you apply this logic to java.lang.Object (assuming the source folder is still called src), you'd get this:

./src/
L___ ./src/java/
|    L___ ./src/java/lang/
|    |    L___ ./src/java/lang/Object.java
The SE I loved is dead
  • 1,517
  • 4
  • 23
  • 27