Advertisement
public class A
{
}
B.java
class B
{
public static void main(String... ar)
{
A ob= new A();
}
}
public class A
{
public static void main(String...ar)
{
System.out.println("Hello");
}
}
D:\Java>javac A1.java
A1.java:1: error: class A is public, should be declared in a file named A.java
public class A
^
1 error
Advertisement
package pack1;
class A
{
}
B.java
package pack1;
class B
{
public static void main(String... ar)
{
A ob = new A();
}
}
Class A and class B exist in the package, pack1. Hence, when class A was accessed in the code of B class, we have received no compile error in the compilation of B.java.
package pack1;
class A
{
}
B.java
package pack2;
class B
{
public static void main(String... ar)
{
A ob = new A();
}
}
B.java:7: error: cannot find symbol
A ob = new A();
^
symbol: class A
location: class B
B.java:7: error: cannot find symbol
A ob = new A();
^
symbol: class A
location: class B
2 errors
}
Class A exists in the package, pack1, while Class B exists in the package, pack2.
Hence, we have received compile errors on the compilation of B.java, clearly stating that the compiler couldn't find class A when accessed in the code of B.java
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement