[Learning Notes] Java Access indicator Public,protected,private, default scope

Source: Internet
Author: User

0. Introduction

Java's access indicator, public,protected,private, can be used to decorate classes and methods by default.

1. The scope is as follows

Public: Other classes can access this class or method

Protected: For inheritance, subclasses can access the method of the parent class's adornment

Private: Used for the class itself, generally decorated member variables

Default: Available in the same package ("friendly" class)

2. If there are multiple classes in the same Java file

(1) Each compilation unit (file) can have only one public class. Each compilation unit has a common interface concept that is made up of that public class table
Up. Depending on your needs, it can have any number of "friendly" classes that provide support. However, if more than one is used in a compilation unit
public class, the compiler will prompt us with an error message. (That is, only one is a public modifier, others are default)

(2) It is possible (but common) to have a compilation unit that does not have any public classes at all. At this point, you can arbitrarily specify the file name as you wish. (That is, it can be the default adornment, that is, no access indicator decorates the class name)

3. If you do not want anyone else to access that class

We now have only two choices for class access: "Friendly" or public. If you don't want anyone else to access that class, you can set all the builders to private, and then use the (singleton) singleton mode or static method to get instances of this class

: Lunch.java//demonstrates class access specifiers. Make a class effectively private//with private constructors:  class Soup {   private Soup () {}   //(1) Allow Creation via static method: public   static Soup Makesoup () {     return new Soup ();   (2) Create a static object and   //return a reference upon request.   (the "Singleton" pattern):   private static Soup PS1 = new Soup ();   public static Soup access () {     return PS1;   }   public void F () {}}  class Sandwich {//Uses Lunch   void F () {new Lunch ();}}  Only one public class allowed per File:public class Lunch {   void Test () {     //Can ' t do this! Private constructor:     //! Soup priv1 = new Soup ();     Soup priv2 = Soup.makesoup ();     Sandwich f1 = new Sandwich ();     Soup.access (). f ();   } } ///:~  

4. Exceptions

In the same Java file, we now have only two choices for class access: "Friendly" or public, but if it is an inner class, it can be decorated with protected and private.

  

[Learning Notes] Java Access indicator Public,protected,private, default scope

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.