Reputation: 1355
I have a package called "test" and in that have a public class that contains the main method in a file called ABC.java.
package test;
public class ABC{
public static void main(String[] args) {
new T1();
}
}
In that same package "test" I have two default classes T1 and T2 in a file called T.java
package test;
class T1 {}
class T2 {}
when i try to compile it it says cannot find symbol new T1()
. When I put T1 in a separate file T1.java
then it compiles fine. Why java is unable to find package private class in the same package.
Upvotes: 1
Views: 393
Reputation: 8652
javac will automatically compile all the linked file used in the file you are compiling if there .class
files are not found. Like in your case ABC.java
. But one thing to notice is javac will not search for all the files with .java
extension to be compiled. But it will look for the file name with the same name as the class. Like in your case T1
.
So if you will compile T.java
and then compile ABC.java
it will run as expected. But if you compile ABC.java
and not T.java
compiler will not find T1.class
then it will look for T1.java
, but it will not found it too, which will give you an error. On the other hand if you will rename T.java
to T1.java
it will work as expected.
Upvotes: 2
Reputation: 14217
In Java
, When a class file name(T1.java) is same as class's name(T1) without public
keyword, this class is public under this package(test)'s class.
Upvotes: 0