I have the following two classes
package one;
public class Student
{
//Some code
}
package two;
public class Test
{
public static void main(String args[])
{
Student s = new Student();
//Some code
}
}
Even though the "Student" class has public access modifier, whenever I try to create an object for the Student class, in the Test class, which is from another package, eclipse points out error saying either I need to import the student class or create a new class.
I thought if a class is declared public, it can be accessed from any where. But why does eclipse call it an error?
You don't have an import
statement, so the compiler doesn't know that Student
is meant to refer to one.Student
. You can either use:
import one.Student;
or
import one.*;
... or just fully-qualify the name when you create the object:
one.Student s = new one.Student();
This isn't a matter of accessibility - it's the compiler not knowing how to resolve the identifier Student
into a fully-qualified class name.
See more on this question at Stackoverflow