简体   繁体   中英

java using multiple classes and compiling

I'm just new to java.I made two java files. One is Main.java which contains the main method and the other one is Second.java which contains some fields and methods. After that I created the object of "Second" in the Main.java and call the fields and methods from Second. And then I compiled both of the file and it output the result of the fields and methods from Second. Then I put one more method in the Second.java file and call that method in the Main.java. At that time, I only have to recompile the Main.java file and that method was called. I don't have to recompile the Second.java file. How's that work? Doesn't the Second.java file has to be recompiled after some modification?

( I used the notepad for better understanding of java compiling process)

javac automatically recompiles dependencies when it finds a corresponding .java file.

You will notice that if you delete both .class files and build only Main.java with javac Main.java , then Second.class will still be generated.

If you then delete or move Second.java but leave Second.class in place, and modify Main.java to call a new method, then javac will find the old class, be unable to recompile it, and give you the error about missing methods that you expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM