File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQPlus??" Watch "JQPlus??" New topic
Author

JQPlus??

Sam Cooper
Greenhorn

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 */
}
public
protected
friend
no access modifier
private
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
Thanks
Valentin Crettaz
Gold Digger
Sheriff

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...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[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: http://aspose.com/file-tools
 
subject: JQPlus??