Why does Java generate Multiple .class files on compilation?

后端 未结 3 1574
逝去的感伤
逝去的感伤 2021-01-12 06:57

In Java, on compilation we get a .class file for each class( including nested classes and interfaces) defined in the source file.

What is the reason for this multiple

3条回答
  •  生来不讨喜
    2021-01-12 07:31

    The JVM needs to be able to find the code for a given class, given its name. If there's potentially no relationship between the source filename and the code filename, and you want the code filename to be based on the source filename, how would you expect it to load the code?

    As an example: suppose I were to compile Foo.java which contains class Bar.

    Another class then refers to Bar, so the JVM needs the code for it... how would you suggest it finds the file?

    Note that in .NET there's a separate of unit of deployment called the assembly - and a reference to a type includes the assembly name as well, but that's slightly different from what you were proposing.

提交回复
热议问题