I got a problem that is very strange. I have two source files saved in the same directory: E:\Java Programmes
First one: A.java
package util;
public class A {
public int add(int x, int y){
return (x + y);
}
}
Second one: B.java
import util.*;
public class B {
public static void main(String args[]){
A a = new A();
System.out.println("Result: " + a.add(3, 5));
}
}
I compiled these two files and got problem as follow:
*compiling file A.java is ok, no problem.
E:\Java Programmes>javac -d "E:\Java Programmes" A.java
*but got problem with B.java
E:\Java Programmes>javac B.java
B.java:5: cannot access A
bad class file: .\A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
A a = new A();
^
1 error
*The strange thing is that when i modify the file B.java as follow, it can run well
import util.A;
public class B {
public static void main(String args[]){
A a = new A();
System.out.println("Result: " + a.add(3, 5));
}
}
Output = Result: 8.
I keep wondering why it is like that. Because the statement "import util;*; " does not work well while the statement import "util.A ; " can work well. Please, anyone, explain it to me. Thannks a lot.