1

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)

3
  • Could you show us your code? At least the modifications? Commented May 9, 2022 at 19:12
  • 1
    Second.java would need to be recompiled for you to use the new method in Main. Commented May 9, 2022 at 19:14
  • Please provide enough code so others can better understand or reproduce the problem. Commented May 9, 2022 at 19:15

1 Answer 1

2

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.

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

1 Comment

Thanks all. I understand how's that work with all of your helps. I'm really appreciate all of you.

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.