• Post Reply Bookmark Topic Watch Topic
  • New Topic

package  RSS feed

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test is declared as follows
package A;
class Test {
protected static int x = 5;
public static void main(String args[]) {
System.out.println("TEST");
}
}
and class Test1 is declared as follows
package B;
import A.*;
class Test1 extends Test {
public static void main(String args[]) {
System.out.println("TEST1");
System.out.println(x);
}
}
why I am getting a compiler error? Is it necessary that Test class should be declared as public? But I am importing the entire package here isn't?
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error message you're getting is this:

you defined class 'Test' as having package (default) accessiblity, so in order to access that class, you need to be in the same package, like the error message says. Try granting it public or protected access and see what happens.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Test has default access. It can be accessed only from the package in which was declared.
This explains more:
JLS 6.6 Access Control
The import sentence only buys you the possibility to name a type without the need of the qualifying packages names before it. An error ocurrs if the types is not accessible.
More on that:
JLS 7.5 Import Declarations
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jose & Jessica, Jessica You said,

you defined class 'Test' as having package (default) accessiblity, so in order to access that class, you need to be in the same package, like the error message says. Try granting it public or protected access and see what happens.

where you want me to give protected access? I know I can't do it for Test since it is a top level class. No protected access allowed. But public works. Thanks
[ June 10, 2002: Message edited by: Thiru Thangavelu ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!