0

class A and B are independently save at the folder named "myjava", when i add the "package" at the first line, class B can not compile and prompt the following message but class A can compile sucessfully:

B.java:3: error: cannot find symbol class B extends A{ ^ symbol: class A 1 error

//package myjava;
class A{
  void funcA(){
  System.out.println("A");
  }
}

//package myjava;
class B extends A{
  void funcA(){
  System.out.println("B");
  }
}

i wonder why adding the keyword "package" can not compile successfully.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
wayenng
  • 1
  • 1
  • 4
    How are you trying to compile the code? If you're using `javac`, **exactly** what command and what working directory are you using? – Matt Ball Jun 18 '13 at 02:54
  • Add some details on how are you compiling the classes . It might be the case that you are not including the class A (which is already compiled) in your classpath (by using -cp option if using javac). – Shailesh Pratapwar Jun 18 '13 at 03:14
  • 1
    [This][1] answer may be helpful to you. [1]: http://stackoverflow.com/questions/12652899/difference-between-package-and-directory-in-java – Darshan Mehta Jun 18 '13 at 03:24

1 Answers1

0
package myjava; 
public    class A{
 void funcA(){
 System.out.println("A");
}
}

--------------A.java--------------------

 package myjava;
 public class B extends A{
  void funcA(){
System.out.println("B");
}
}

----------B.java------------

Now compile

javac -d . A.java
javac -d . B.java

Hope this helps

LMK
  • 2,882
  • 5
  • 28
  • 52
  • I used the command as follow: javac A.java ---> can be compiled javac B.java ---> can not be compiled even have the "public" or without in front of "class B extends A"... anyway thanks~ – wayenng Jun 18 '13 at 15:25
  • If your class having package you have to compile c:/> javac -d . A.java – LMK Jun 19 '13 at 05:01