compilation - I have some questions about Java compiler -


i have questions java compiler.

my current directory this.

├── hoge.java ├── sample.class ├── sample.java ├── pattern01 │   └── com │       └── cat │           └── hoge.class └── pattern02     └── com         └── cat             └── hoge.class 

----- sample.java -----

import com.cat.hoge;  public class sample {      public static void main(string[] args) {         system.out.println("hello!");         hoge h = new hoge();         h.call();     } } 

----- pattern01 -----

package com.cat;  public class hoge {      public void call() {         system.out.println("com.cat");         system.out.println("pattern01");     } } 

----- pattern02 -----

package com.cat;  public class hoge {      public void call() {         system.out.println("com.cat");         system.out.println("pattern02");     } } 

i compiled sample.java this.

$ javac -cp pattern01 sample.java  

and execute this.

$ java -cp .:pattern01 sample hello! com.cat pattern01  $ java -cp .:pattern02 sample hello! com.cat pattern02 

both pattern01 , pattern02 ended.

but compiled pattern01. why did program end pattern02?

what compiler check? compiler check class name ?

classes resolved @ runtime. compiled client class (sample) version of hoge class in classpath, , ran version of class. since class still compatible (same package, same name, same method signature), goes well.

that's allows compiling classes given version of library (or jdk), still run version of same library (or jdk). if wasn't possible, nightmare build reusable libraries, since every library have compiled against every version of jdk , every version of every dependant library.


Comments