my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQPlus?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQPlus??" Watch "JQPlus??" New topic


Sam Cooper

Joined: Feb 21, 2002
Posts: 14
How can you declare i so that it is not visible outside the package 'test'.
package test;
class Test
XXX int i;
/* lot of code */
no access modifier
One of the answer is no access modifier, what is the other possible choice.
Is it public? Does the access modifier of the method override the class modifier.Hope my question makes sense. In other words if declare a method private in public class, the net access permission of the method is public
or is it class modifier overrides the method modifier
Any suggestions are welcome
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
The answers are:
no access modifier and private.
By declaring i:
- public, i is visible in any class of any package
- protected, i is visible in the whole package in which class Test is contained as well as in any subclasses of Test in or out of the package in which class Test is contained
- friend, it won't compile since friend is not a legal java access modifier
- with no access modifier we are declaring i as having a so-called "default" access where only classes within the same package will be allowed to refer to i
- private, i will only be avalaible within the scope of class Test, and thus, only in the package where class Test is contained.
Moreover, the access modifier of a method does not "override" the one of the class. It is exactly the opposite actually. If you cannot access a class, then you surely cannot access methods in it even if they are declared public. If you can access a class then only you will be able to access methods declared in that class, provided the access modifier of the method permits the access.
Think of the class as a house and the methods as rooms in the house. If you cannot enter the house, you won't be able to enter rooms in that house. But if you can enter the house, you may be allowed to enter some rooms that have public access and not allowed to enter others because they are closed (read private) and you have no way to get the key...

[Blog] [Blogroll] [My Reviews] My Linked In
Arun Pai
Ranch Hand

Joined: Mar 11, 2002
Posts: 143
Beautifully explained Valentin, you are making our life easier here in this forum. Though I knew the answers, your concepts are strong with good analogies.
I agree. Here's the link:
subject: JQPlus??
jQuery in Action, 3rd edition