• Post Reply Bookmark Topic Watch Topic
  • New Topic

java classes with public access  RSS feed

 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I just started learning about packages and wrote my first program with code in two different files. But I am not able to understand why the error occurs:




the above code is stored in a file called P.java . please note that the class P is declared public.

now please look at the code below which makes use of the package 'tools' in the above file.


then i get the error:
onevalue(int) is not public in tools.P . i wonder why as i have declared P as public. so shouldnt its methods and everything in it be accessible outside that file too?(bruce eckel says so).
please give me a detailed explanation about what i am missing.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The no-modifer (or default or package-private) onevalue() method will be accessible only from classes in the same package. Read more about controlling access here.

i wonder why as i have declared P as public. so shouldnt its methods and everything in it be accessible outside that file too?(bruce eckel says so).

You shall post the sentence from the respective book which you think might be wrong.
 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking in java by bruce eckel(lp edition) on page 256 says :

"when you use the public keyword,it means that the member declaration that immediately follows public is available to everyone in particular to the client programmer who uses the library. "
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thejwal pavithran wrote:"when you use the public keyword,it means that the member declaration that immediately follows public is available to everyone..."

OK, and the important part of that sentence is "the member declaration that immediately follows public". I've underlined the key words for you.

Specifically, the public in the class declaration only makes the class public, NOT its members.

Winston
 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

OK, and the important part of that sentence is "the member declaration that immediately follows public". I've underlined the key words for you.


yeah reading on i understand it..i was stuck with the c++ idea where once you write 'public:' everything which follows is public until another access specifier is specified. Thanks for the help guys!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!