The eighth chapter summarizes

Source: Internet
Author: User
Tags modifiers

Access modifiers

Private access rights: this class

Default access rights: Same package

Protected access rights: Same bun Category

Public access rights: all can

Packaging

Makes the member variables in a class private and becomes data-hidden.

Benefits of Encapsulation:

A member variable of a class can be read-only or write-only;

A class can have a holistic control over what is stored in its member variables.

The user of the class does not need to know how the class stores the data.

Static members

The static method cannot manipulate any properties and methods that are not static.

Cause: 1, static is the class level, regardless of the object.

2, static modification is the first family, the non-static at this time has not been loaded into memory, so can not access.

The tool method for writing tool classes is modified with static.

A non-static method is one that can manipulate any property and method of static.

Initialize block

Instance initialization block: Automatically executes when the object is generated, after the constructor of the parent class, before the class constructs the method

The usage is very small, because the code can be written to the first few lines of this class construction method.

Static initialization blocks

static{

Automatically executes when the class is loaded and executes only once.

Use is very high, usually put some of the pre-run preparation work here, let it in the loading period to complete the preparation.

Inner class

member Inner class

1, can have access modifiers

2. class file name: External class class name $ internal class class name. class

3, is a complete independent class

4, in any non-static method of this class can be new out, in a third-party class must use the external class object. The new method produces the inner class object.

Local inner class

1. Cannot have access modifiers

2. class file name: External class class name $ ordinal + inner class class name. class

3, is a complete independent class

4, in this method can produce objects, and then use.

Static Inner class

1, can have access modifiers

2. class file name: External class class name $ internal class class name. class

3, is a complete independent class

4, in any method of this class can be new out, in the third party class to new out, no need to produce an external class object, just use the external class name. The way is possible.

Anonymous inner class

Because there is no class name, it is used to tell the JVM its structure when the object is produced.

can only be used 1 times.

class file name: Outer class class name + ordinal. Class

The eighth chapter summarizes

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.