-1

I get an error in my code that can't find the symbol and I've searched everywhere but still can't find the solution. this file is separate

    class P1 {
    protected void aFancyMethod() {
        System.out.println("what a fancy method");
    }
}

public class P2 extends P1 {
    public static void main(String argv[]) {
        P2 p2 = new P2();
        p2.aFancyMethod();
    }
}

my error same...

    P2.java:3: error: cannot find symbol
public class P2 extends P1 {
                        ^
  symbol: class P1
P2.java:7: error: cannot find symbol
        p2.aFancyMethod();
          ^
  symbol:   method aFancyMethod()
  location: variable p2 of type P2
2 errors
  • 1
    It looks like those are two separate files, despite what your sample shows. If the file `p2.java` needs something in `p1.java`, you have to import it. – Tim Roberts Oct 13 '21 at 06:40
  • @TimRoberts - But there are restrictions on importing from the default package; see https://stackoverflow.com/a/2030159/139985. – Stephen C Oct 13 '21 at 07:21

1 Answers1

0

Assuming that you have separate file for classes p1 and p2... if they are named p1.java and p2.java and they are in same directory then it would be fine doing:

javac p2.java

However if you have P1.java (with capital P) or any other filename for class p1 then javac will not be able to find source code for compilation of class p1 when compiling class p2.

Check your filenames. (I don't know whether this problem will exist on non-case-sensitive os like windows.)

Alternative this should be fine regardless of filename:

javac *.java

Anyway the recommendation is:

  • Class names start with capital letter
  • Each class is in it's own .java file.
  • name of java file matches with classname exactly including letter case.
One.Punch.Leon
  • 602
  • 3
  • 10