Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

B. The Top Ten Topics that almost made i... > #4 Access Levels and Access Modifier...

#4 Access Levels and Access Modifiers (Who Sees What)

Java has four access levels and three access modifiers. There are only three modifiers because the default (what you get when you don’t use any access modifier) is one of the four access levels.

Access Levels (in order of how restrictive they are, from least to most restrictive)

image with no caption

Access modifiers

public
protected
private

Most of the time you’ll use only public and private access levels.

public

Use public for classes, constants (static final variables), and methods that you’re exposing to other code (for example getters and setters) and most constructors.

private

Use private for virtually all instance variables, and for methods that you don’t want outside code to call (in other words, methods used by the public methods of your class).


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial