• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

package

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic