Search code examples
javacompilationjavac

How does java compile source code to bytecode when there are multiple class files?


I learning java. I want understand how this compilation process is done. Imagining how java compiler would compile is simple when there is one file. It would compile line by line. How about having multiple class files depending on each other. How would be the flow of compilation. Any resources are appreciated.


Solution

  • Compiler don't translate source code into bytecode line by line. They parse the source, construct an intermediate representation, then transform that intermediate representation (which might need information from other classes) in multiple passes, then generate bytecode.

    For the transformation phase it doesn't need the whole bytecode of other classes, just information about the structure of other classes (fields and methods with types and signatures). That information can be gathered from class files or by constructing the intermediate representation of a class from its source.

    https://en.wikipedia.org/wiki/Compiler