Java Classes and objects

Source: Internet
Author: User

Class-to-object relationships

By extracting the common characteristics and behaviors of a class of things, the feature is extracted as a property, and the behavior is extracted as a method, thus forming a class---class is the generalization of the object.

Create an instance/object of the corresponding class by using the New keyword---object is a class-specific

member variables and local variables

Difference:

1. Define the location differently. Member variables are defined outside of the method in the class. A local variable is defined within a method or statement.

2. Scopes are different. Member variables function within the entire class, and local variables function within the current method or statement defined.

3. Storage location is different. Member variables are stored in heap memory and are automatically assigned initial values in heap memory; Local variables are stored in stack memory and need to be initialized manually

4. Life cycle is different. Member variables are generated as objects are created, destroyed as objects are recycled, and are not necessarily recycled immediately after they are exhausted, but are recycled at some point in time. Local variables are created when the method or statement executes, and immediately after the execution of the method or statement is completed.

constructor function

A function that has the same name as a class in a class and does not return a value type

Constructors can be overloaded.

The constructor can write a return statement to avoid some problems that do not conform to the logic of the facts

this keyword

Which object is used, this represents which object---can be considered a virtual object---this represents a reference to the current object

Note: All non-static properties and methods in Java are called by the object.

The This statement---call other constructors in constructors in this class---the This statement must be placed in the first row of the constructor

Anonymous objects

An object without a name is called an anonymous object---when the method value is called once

An anonymous object can be passed as a parameter

Local code block

A block of code enclosed in {} inside a function or statement is called a local code block.

function---can limit the use of variables and life cycle, improve the utilization of stack memory

Java Classes and objects

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.