Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding "javac" compiler

 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please consider the below classes

Superclass (This is in "food" package):
//Fruit.java
package food;
public abstract class Fruit{ }

Subclass( This is in default package);

//Apple.java
import food.Fruit;
class Apple extends Fruit{ }

Now I just compiled the Apple.java in the default package. But when I went into the "food" folder I see that "Fruit" class is also compiled. It compiled by itself. I didn't compile it.

My question: When we compile a sub-class will it compile the super class also. or How it works? Please explain.

Thanks.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we compile a sub-class will it compile the super class also. or How it works?

It does it if necessary. To compile Apple.java, Fruit.class is needed - if the class file isn't present the compiler will look for its source code in the same source directory where Apple.java lives. If it doesn't find it, you will get an error.
 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fine. Thank you.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester Burnham wrote:It does it if necessary. To compile Apple.java, Fruit.class is needed - if the class file isn't present the compiler will look for its source code in the same source directory where Apple.java lives. If it doesn't find it, you will get an error.
Even if the class-file of Fruit is found it will recompile Fruit. If the source of Fruit is unavailable then it will use the existing class-file.
 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter Oet
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Even if the class-file of Fruit is found it will recompile Fruit


For more clarity : only if Fruit.java is latest than .class file, else javac uses the class file.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha I didn't know that but that makes perfect sense.
 
Loganathan Karunakaran
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Seetharaman.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loganathan Karunakaran wrote:Thank you Seetharaman.

You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic