So I thought I understood packages, but turns out I don't.
Classes inside a package: I have this folder structure:
mypackage/mysubpackage
. Inside mysubpackage folder I have 2 classes:package mypackage.mysubpackage; public class Class1 {...}
and
package mypackage.mysubpackage;
public class Class2 {...}
However, when I compile Class1 (which uses methods from Class2) using javac Class1.java
inside the directory mypackage/mysubpackage
, it can't see Class2:
Class1.java: error: cannot find symbol
Class2 c = new Class2();
^
symbol: class Class2
location: class Class1
It works fine if I run javac Class1.java
in the directory that contains mypackage/mysubpackage
. Shouldn't the compilation work inside mysubpackage folder?
Classes in another package: Now, I have another class with methods that I want to be accesible to all the subpackages, so I create a final
Commons.java
insidemypackage/commons
:package mypackage.commons; public final class Commons { public static double method() {...} ... }
And then I update Class2 importing that class so that I can use its methods inside the class:
package mypackage.mysubpackage;
import mypackage.commons.*;
public class Class2 {...}
Now it doesn't find the method I defined in the final class:
./mypackage/mysubpackage/Class2.java: error: cannot find symbol
double var = method();
^
symbol: method method()
location: class Class2
Shouldn't if find it? I think I'm importing it correctly, the methods are static and the class is final. Why doesn't it recognize it?
Cheers!