GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.oracle.com — Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects) Documentation
The Java™ Tutorials
Hide TOC
Controlling Access to Members of a Class
Trail: Learning the Java Language
Lesson: Classes and Objects Section: More on Classes The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. Controlling Access to Members of a ClassAccess level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:
A class may be declared with the modifier At the member level, you can also use the The following table shows the access to members permitted by each modifier.
The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member. Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have. Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related. ![]() Classes and Packages of the Example Used to Illustrate Access Levels The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.
Tips on Choosing an Access Level: If other programmers use your class, you want to ensure that errors from misuse cannot happen. Access levels can help you do this.
About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.
Previous page: Using the this Keyword
Next page: Understanding Class Members | ||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |