Java series Learning (7)-object-oriented and java object-oriented

Source: Internet
Author: User

Java series Learning (7)-object-oriented and java object-oriented

1. Differences between member variables and local variables

(1) different positions in the class

   

Member variable: out-of-class Method

Local variable: In the Method

(2) locations in memory are different

   

Member variable: In the heap

Local variable: in the stack

(3) different lifecycles

   

Member variable: It exists with the creation of the object and disappears with the disappearance of the object.

Local variable: It exists with the call of the method and disappears with the completion of the call of the method.

(4) Different initialization values:

   

Member variable: with default values

Local variable: no default value. It must be defined and assigned values before use.

2. Anonymous object

(1) objects without names

(2) Application scenarios:

   

A: call A method only once.

B: It can be passed as an actual parameter.

3. private keywords

(1) significance of private:

   

Member variables and member methods can be modified.

(2) features:

   

Private-modified members can only be accessed in this class.

(3) private applications:

   

Give all member variables to private and provide the corresponding getXxx ()/setXxx () method.

4. this keyword

(1) indicates the reference object of the current class.

(2) this Application Scenario

5. Constructor

(1) role:

   

Used to initialize object data

(2) format:

   

A: The method name and class name are the same.

B: There is no return value type, and no void is available. All methods can actually have [return ;]

C: No Return Value

(3) constructor

   

A: If no constructor is written, the system provides A default constructor without parameters.

B: If we provide a constructor, the system will not provide a default constructor.

(4) assign values to member variables

   

A: setXxx ()

B: Construction Method with Parameters

6. Understanding of static keywords

(1) Static meaning:

   

Member variables and member methods can be modified.

(2) Static features:

   

A: loading with classes

B: takes precedence over object existence.

C: shared by all objects of the class

D: It can be called by class name.

(3) Static Memory Diagram

   

Static content is in the static area of the method area.

(4) Static considerations:

   

A: There is no this object in the static method.

B: static access only

(5) differences between static variables and member variables

   

A: different:

Static variables: belong to the class and class variables]

[Member variables: objects, object variables, and instance variables]

B: memory locations are different:

Static variables: static areas in the Method Area]

[Member variable: heap memory]

C: different lifecycles:

Static variables: static variables are loaded with the class and disappear with the disappearance of the class]

[Member variable: The member variable exists with the creation of the object and disappears with the disappearance of the object]

D: Different call methods:

Static variables: can be called by Object Name or class name]

[Member variable: can only be called by Object Name]

① If the class does not declare the public permission, the method in the class will be public.

7. Understanding of the main method

   

Public: Maximum permission

Static: You can call it without creating an object.

Void: return is returned to the caller. It does not make sense to return to jvm.

Main: is a common name (many languages start with main)

String [] args: Accept data, provide program flexibility, and now has more powerful metrics

8. Object creation process

Analysis:

1. Load the Student. class file to the memory, open up space for stu in the stack memory, and start to press the stack

1.1 apply for space for Student objects in heap memory

1.2 The heap memory has an address pointing to the Method Area, which contains methods in the Student class.

2. Prepare to call the setName (String sName) method and press setName () on the stack. After the call is completed

3. Prepare to call the getName () method and press getName () on the stack. After the call is completed

4. Prepare to call the SayHi (String sName) method and press SayHi (String sName) to the stack. After the call is complete

5. End the calling of main (), and the main () outputs the stack.

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.