Java_ class and Object 03_ encapsulation and private, this, super, static keywords

Source: Internet
Author: User

A Package: A member variable in a class is private, which provides a public get () or set () for a private member variable.

Benefits of Encapsulation:

I. Hide implementation details and provide a common way to access

Ii. improved reusability of the code

Iii. Improving safety

The principle of encapsulation (thought):

I. Hide content that does not need to be provided externally

II. Hide attributes, provide public methods to access them

The embodiment of encapsulation: private is a manifestation of encapsulation.

B Private:

I. is a permission modifier

II. Members can be decorated (member variables and member methods)

III. Members that are private modified can only be accessed in this class

The C this keyword: represents an object reference for the current class.

Refers to member variables and member methods in the current class .

    Private String name;      Public Static void Main (string[] args) {        this///  Error, the Main method is static and the This keyword cannot be used.      }    public  String getName () {        return// This is actually implied here.    

D Super Keyword: The identity that represents the storage space for the parent class. (It can be understood as a reference to the parent class , through which the members of the parent class can be accessed.)

E Static Keyword:

1. You can modify member variables and member methods . a static member can be called directly using the class name (interface name) or by an object of the class.

Modifier member Variable: static variable .

I. All objects in the class share a static variable.

Ii. static variables are initialized only the first time the class is used , and are no longer initialized when the class is used for the second time.

Modify Member Method: Static method.

I. static methods cannot use this with the Super keyword .

Ii. static methods can only invoke static members of the current class directly. A non-static member that wants to be called must be called through the object of the class (because the static method does not accept the implied this).

private String name;

static String num;

Direct invocation of a static method:

To invoke a static member:

Test.num = ""; Called by the class name

This.num = ""; Error, the static decorated member method (that is, still method) cannot use this with the Super keyword

num = ""; is equivalent to Test.num = "";

You can also call through the object of the class

To invoke a non-static member:

THIS.name = ""; Error, static decorated member method cannot use this with the Super keyword

Test.name = ""; Error, name is not static

name = ""; Error, name is not static, so it is equivalent to THIS.name

Can only be called from an object of the class

Direct invocation of non-static methods:

To invoke a static member:

Test.num = "";

This.num = ""; The principle seems to be the same as above, this: is the object reference of the current class

num = ""; Call method and static method are the same principle of direct invocation

You can also call through the object of the class

Calling non-static members

Test.name = ""; Error, only static members can be called with the class name (interface name)

name = ""; Equivalent to this.name = "";

You can also call through the object of the class

Iii. a non-static method can invoke a static member directly.

2. Static characteristics:

A: Loaded with the load of the class

B: Priority and object presence

C: Shared by all objects of the class

D: Can be called by the class name:

Called by the name of the object, or by the class name, is recommended by the class name.

3. Static memory diagram: Static content in the method area

4. Static precautions;

A: There is no this object in the static method

B: Static can only access static (code tested too)

5. The difference between a static variable and a member variable

A: Belong to different

Static variable: Belongs to class, class variable

Member variables: belong to object, object variable, instance variable

B: Different memory locations

Static variables: Static area of the method area

Member Variable: heap memory

C: Different life cycle

Static variables: Static variables are loaded as the class is loaded and disappear as the class disappears

Member variables: Member variables are present as the object is created and disappear as the object disappears

D: Call Different

Static variables: Can be called by object name, or by class name

Member variable: can only be called by object name

Java_ class and Object 03_ encapsulation and private, this, super, static keywords

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.