1

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to compile a class A that depends on class B, the compiler first looks at B, then comes back to A. This also works in more complex situations where A depends on B and C, or A depends on B that depends on C.

The really interesting situation is when A depends on B which depends on A. Lookup 'circular dependendy' and you will see a wealth of discussions - for different programming languages, or in Java for classes, jars etc.

Examples:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.