Older Dick Silk Self-study note--java 0 basics to rookie--016

Source: Internet
Author: User

Encapsulation, private, constructor, this, initialization process of class, static, static variable and member variable difference

1. Encapsulation

Encapsulation Overview: Hides the properties and implementation details of an object, providing public access only to the outside.

Packaging Benefits: Improved code security, reusability.

Encapsulation principle: To hide the attributes, etc. that do not need to be provided externally, to provide public methods for their access.

2. Private

Private: The access modifier, which can be decorated with member variables and member methods, is accessible only in this class.

3. Constructor function

Constructor: The initialization method of the object.

Definition Format: Public object name, such as: public Fin (); , Public Fin (int.);

Note: If you do not have a custom constructor, the system provides the parameterless constructor by default.

If you already have a custom constructor, the system no longer provides a parameterless constructor.

4, this

This: Refers to the object reference of the current class, that is, who calls this method, and this in the method represents who.

1 classstudent{2     Private intAge ;3 4      PublicStudent () {5     }6 7      PublicStudent (intAge ) {8          This. age=Age ;9     }Ten  One      Public intGetage () { A         returnAge ; -     } -  the      Public voidSetage (intAge ) { -          This. age=Age ; -     } - } +  - //This.age is the member variable age, which avoids the member variable being hidden by the local variable with the same name

5. Initialization process of class

(1) Load the. class file.

(2) Open up memory space in the stack for object variables.

(3) Open up memory space for objects in the heap.

(4) The member variable is initialized by default.

(5) member variable display initialization.

(6) Executes the constructor.

(7) Assigns the object address to the object variable.

6. Static

static modifier.

Feature: Precedence over the existence of the object, loaded with the load of the class, is shared by all objects of the class, and can be called through the class name.

1 classliverpool{2      PublicLiverpool () {3System.out.println ("1");4     }5 6     Static intn=Ynwa ();7 8     Static intYnwa () {9System.out.println ("2");Ten         return1892; One     } A } -  - classfin{ the      Public Static voidMain (string[] args) { -Liverpool liv=NewLiverpool (); -     } - } +  - //Output 2 First, then output 1

Note: This is not present in the static method, and only static methods and member variables can be accessed.

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

belong to different: static variable, class variable.

Member variables, instance variables.

Different memory locations: Static variables, static zones.

Member variables, heap memory.

Memory time is different: Static variables are loaded as the class loads.

Member variables, created as objects are created.

Called in different ways: static variables, called by the class name.

A member variable, called through an object.

Older Dick Silk Self-study note--java 0 basics to rookie--016

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.