Reputation: 1107
How exactly is the Java compiler fetching code from other files (using "new" and "extends") without direct reference to those files (by only naming the classes)? is the compiler essentially reading all the Java files in the directory looking for relevant classes to include?
Example: File 1:
public class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
File 2:
public class Student extends Person {
...
...
Person you = new Person("foo");
Upvotes: 0
Views: 151
Reputation: 23035
When you reference a class called Person
the compiler searches for a file called Person.class
.
This search is done in each directory specified in the CLASSPATH
(both the environment variable and the classpath specified in the arguments of java
).
Note that the compiler first needs to know to what package a class belongs, so you can have two classes with the same name in different packages and still it will find the correct one (the one of the package you imported or that you fully qualified).
So if your class is actually: com.myorg.Person
it will search in this classpath for a file:
com/myorg/Person.class
Upvotes: 3