Class, method, constructor, and variable Introduction

Source: Internet
Author: User
Class

Class is the smallest unit of Java, and methods, constructors, and variables must all be in the class.

Syntax: [Modifier] class name {

// Multiple fields;

// Multiple Constructors;

// Multiple methods;

}

[Modifier]: Public | final | optional abstract, which can be omitted.

Class Name: it can be a valid identifier. It is recommended that it be a meaningful word, with each initial letter capitalized.

Method

Methods cannot exist independently, either belong to an object or a class. classes or instances must be used as callers.

Syntax: [Modifier] Return Value Type method name (parameter list ){
// Executable statement;
}
Modifier: can be omitted
Public | protected | private
Static
Abstract | final
Type: Basic Data Type and reference type. If the return value type is declared, a valid return statement is required to return a variable or expression. If no return value exists, use void to declare
Method Name: identifier. It is recommended that you use a verb. The first letter is lowercase and the last letter is uppercase.
Parameter List: 0 ~ N groups of parameter types are separated by commas (,).

Parameter transfer mechanism: Value transfer. The real parameter is a copy, and the referenced variable is the first memory address.

Variable number of parameters: 1. When the number of method parameters is unknown, add three points (...) after the last parameter type (...), it indicates that multiple parameter values (including 0) can be accepted, and multiple parameter values are passed as arrays. 2. A method has only one variable-length parameter. Multiple parameters or an array can be input.

Recursive Method:A method itself is called in the body. recursion must be performed in a known direction. Otherwise, an endless loop similar to an endless loop (no wireless loop) will occur)

Method overload: The same class contains two or more methods with the same name, but different parameters. It is irrelevant to other parts of the method.

Constructor

Create a class instance using the new keyword and perform initialization.

Syntax: [Modifier] constructor name (parameter list ){
// Executable statement;
}
Modifier: Public | protected | private (Public | protected | private) can also be omitted
Constructor name: must be the same as the class name
Parameter list: the format and method are identical.

Initialization: 1. The class contains at least one constructor, which is not created by the programmer. By default, the system provides a non-argument constructor.
2. When a programmer creates a constructor, the system will not provide a non-argument constructor. To retain the parameter-free constructor, you can create another parameter-free constructor.
3. creating any object always starts from the constructor of the top-level class in the inheritance tree where the class is located, runs down in sequence, and finally executes the constructor of the class.

Heavy Load: 1. constructor overload: there are multiple constructors in the same class with different parameter lists. (The names are all class names)
2. When a constructor completely contains another constructor, you can use the this keyword to call another constructor.

Variable member variable

1. The variable defined in the class is field. A class cannot define two member variables with the same name at the same time.

2. Do not use static modifier to call an instance field, or use static modifier to call a class feild.

Syntax: [Modifier] field name of field type [= default value];

Modifier: can be omitted
Public | protected | private
Static | final
Type: Basic Data Type and Reference Data Type
Variable name: Valid identifier. We recommend that you use meaningful words. The first letter is followed by the first letter.
Default Value: Optional

Initialization:1. The member variables do not need to be initialized. The system initializes them by default. The default value of string type is null. The default value of INTEGER (including char) is 0. The default value of float type is 0.0. The default value of boolean type is false.
2. All class instances use the class feild when the class is feild. We recommend that you use the class as the main tune when the class is feild.

Local variable

1. variables defined in the method, including the form parameters, method local variables, and code block local variables.
2. Two local variables with the same name cannot be defined simultaneously in a method. Local variables in different code blocks in a method can have the same name.
3. The local variables in the method have the same name as the member variables. The local variables overwrite the member variables. If you need to reference the overwritten member variables in this method, you can use this or a class name.

Initialization: Except the form parameter, other variables must be initialized after definition. The variable is valid only in the current scope.

Class, method, constructor, and variable Introduction

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.