0

In Java:

class A{}
class a{}

after compilation : A.class

class a{}
class A{}

after compilation : a.class

Why only one class created? the first we declared only that .class file generated

Please explain the reason in detail.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 12
    Are you running on Windows, by any chance? If so, the file system is case-insensitive, so you *can't* create both `A.class` and `a.class`. On a case-sensitive file-system, I'd expect both files to be created. – Jon Skeet Mar 26 '23 at 07:01
  • As a side-note, please read https://stackoverflow.com/editing-help and preview your post carefully before submitting - all your code was written as non-code, and all your non-code was written as code. (I've fixed it for you now.) – Jon Skeet Mar 26 '23 at 07:03

1 Answers1

-4

Historically in Java, the name (and case) of the "main" public class of the file had to match the file name. So public class A has to be in the file A.java and will compile into A.class and you will run it's main() method using "java A". we have to name the class and the file the same way.

The second class in the file is not public (implicitly protected since no modifier was given) and is not nested under the A class. no one outside the package of class A will be able to use it. therefore it does not need its' own class file.

That is Java's way of implementing the principle of "Encapsulation". you could create classes and sub-classes in your code, and anyone who receives the compiled products of these classes will not be able to see the internal structure of your code.

Tom Elias
  • 751
  • 6
  • 15
  • 3
    Downvoted because this is nonsense. Every class in Java **must be** in its own class file - the [class file format](https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html) doesn't allow storing more than one class in it. – Thomas Kläger Mar 26 '23 at 08:34
  • that is incorrect. you can have multiple classes in a file. public classes and static nested classes get their own file. – Tom Elias Mar 26 '23 at 08:40
  • the code the OP wrote produces only one .class file, but it will not run if any class tries to use the package protected class. – Tom Elias Mar 26 '23 at 08:51
  • @ThomasKläger+ more exactly, each toplevel class and each nested class (static, nonstatic, local, or anonymous) gets a separate file; nested classes have names like `Foo$Sub.class` and `Foo$1.class` so their files don't conflict. It's toplevel classes with different case that conflict on a case-insensitive filesystem like Windows. Also, Java itself does NOT require the _source_ file be publictoplevelname.java although this is _recommended_ and IDEs or other tools often do require it. – dave_thompson_085 Mar 26 '23 at 09:33
  • 2
    You can have multiple classes in a source file (".java" file), but the compiler produces as class file (".class" file) for every class. In the OPs case it is the file system (Windows) that doesn't allow the two files "A.class" and "a.class" to exist at the same time - on Linux and MacOS this is allowed and works. – Thomas Kläger Mar 26 '23 at 09:34
  • @ThomasKläger On Windows it might work as well, as you can configure NTFS to use case-sensitive filenames (this can be configured *per directory*, but the default is case-insensitive filenames. – Mark Rotteveel Mar 26 '23 at 10:56
  • @dave_thompson_085 Java itself actually requires the filename to have the same name as the public class defined in it, otherwise you get a _javac_ error _"X.java:1: error: class Y is public, should be declared in a file named Y.java"_ On the other hand, if there is no public class in a .java file, then you can name it anything you want. – Mark Rotteveel Mar 26 '23 at 11:00
  • 1
    @MarkRotteveel except when you use the feature of running a source file directly (compiling on-the-fly) via `java Source.java`. Then, the source file is allowed to contain multiple public top level classes. – Holger Mar 30 '23 at 11:22
  • @Holger Really? That sounds like a weird and unnecessary relaxation of the rules. – Mark Rotteveel Mar 30 '23 at 11:24
  • 2
    @MarkRotteveel it has always been an *optional* restriction. See [the oldest specification still online](https://docs.oracle.com/javase/specs/jls/se6/html/packages.html#73491): “*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)…*” – Holger Mar 30 '23 at 11:40
  • @Holger True that may be, but I was talking about the relaxation of the rules between `java` compared to `javac`. If you use `javac` on that same file, you'll get error _"Multi.java:9: error: class Other is public, should be declared in a file named Other.java"_, while `java` will just work. – Mark Rotteveel Mar 30 '23 at 12:19