Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is default level class access really package level access?

 
Andrew Luo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the chapter 1 page 13,
" Think of default access as package-level access, because a class with default access can be seen only by classes within the same package..."

But when I do the following in a "woo" dir:

file A.java

file B.java

I got:
cannot find symbol
symbol: class A

for the "A a = new A();" statement

How come foo of B could not access the default level (or package level) access class A? If I remove "package woo;" from both files, B.java compiles.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to import the package where the class is located.
 
Andrew Luo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are in the same package. Why do I have to import? The "import woo.*" doesn't work for either file.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trying the above with

In file B.java.
Also B extends A.

Interesting. Its not working for me either. Just started preparing.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh,,worked ! The parent folder of the Woo directory must be in CLASSPATH
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suhas mandrawadkar wrote:Ahh,,worked ! The parent folder of the Woo directory must be in CLASSPATH


Didnt notice that they were in same package.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I m not able to access a public method in A using dot operator from B
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suhas mandrawadkar wrote:But I m not able to access a public method in A using dot operator from B


Please post the code.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you have posted has other compiler issues as well So please post a working code.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the code that you asked for What other compiler issues ?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suhas mandrawadkar wrote:That is the code that you asked for What other compiler issues ?


Did you try to compile it?
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.. compiles fine without
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Suhas
your class A does not have any method
did you try to compile that code?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suhas mandrawadkar wrote:yes.. compiles fine without


And you didn't mention what is the compiler error
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:
suhas mandrawadkar wrote:yes.. compiles fine without


And you didn't mention what is the compiler error


B.java:7: cannot find symbol
symbol : method eat()
location: class woo.A
a.eat();
^
1 error

Prasad Kharkar wrote:@ Suhas
your class A does not have any method
did you try to compile that code?



Huh ?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are forgetting something in the method declaration for eat
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:You are forgetting something in the method declaration for eat


Now ?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could compile it fine

And please when you edit your older posts- do add a note that you fixed the code. Readers of this thread will be misguided.
 
Suhas Mandrawadkar
Ranch Hand
Posts: 72
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:I could compile it fine

And please when you edit your older posts- do add a note that you fixed the code. Readers of this thread will be misguided.


aye aye Captain .. ! Will keep that in mind from now on
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic