JAVA-object behavior, java Behavior

Source: Internet
Author: User

JAVA-object behavior, java Behavior

In Java programs, all method calls are maintained in a structure called the call stack. The method currently being executed is located at the top of the call stack. After the current method is executed, it is removed from the top of the Call Stack and the control process is returned to the previous method in the stack. In the program, the first method called is main (), which is called by JVM.

If a Java program is a multi-threaded application, it can have multiple call stacks.

When a method is being executed, there are three situations: ① The method returns a value. In this case, a basic data type or reference type is returned to the method caller. ② The method does not return a value. In this case, the return value is declared as void. ③ The method throws an exception to the method caller.

 

 

The method signature includes the method name, parameter list, and data type of the returned value. For example, the main () method signature is as follows:

Public static void main (String [] args)

The method signature does not include any statements in the method body. The signature is only part of the method declaration.

Each part of the method signature is listed in the order in which the method is declared as follows:

■ Access modifier. Possible access modifiers include public, private, protected, or default access modifiers (that is, no access modifier ).

■ Optional modifier. The next part of the method signature is an optional modifier, including static, final, abstract, native, and synchronize.

■ Return value. The method signature must include the type of the return value. If the method does not return a value, use void. Otherwise, you must specify the Data Type of the returned value. The data type of the returned value can be one of eight data types or one reference type.

■ Method name. The method name must appear after the return value. The method name can be a valid Java identifier.

■ Format parameter list. The method name must be followed by a list of formal parameters enclosed in parentheses.

■ Throw an exception list. Method can throw an exception to the method caller.

 

A Method signature includes a list of formal parameters, which are used to declare the type of data passed to the method. The data passed to the form parameter is called the actual parameter.

When the actual parameter is passed to the form parameter, the data of the actual parameter is copied to the form parameter. In programming, the process of copying data between method calls is called value-based call.

Regardless of the type of the actual parameter passed to the method, the relevant formal parameter will get a copy of the data, which is the working principle of calling by value.

 

Methods can be overloaded in Java. If a class has two or more methods with the same name but different parameter lists, it is a method overload (the parameter type is different or the number of parameters is different ).

 

Constructor is a special method in the class. This method is called during Object Instantiation and the purpose of the constructor is to initialize member variables in the object during Object Instantiation.

The constructor differs from the method in that the constructor must meet the following two attributes: ① the constructor name must be the same as the class name. ② The constructor cannot declare the put return value or return the void.

When multiple constructors are added to the class, the method overload rule is applied. Each constructor must have a unique list of parameters in the form to be different from other constructor.

If no constructor is added to a class, the compiler adds a default constructor to the class. The default constructor is limited by a public access modifier, with no formal parameters and no action is required.

 

If a constructor uses the this keyword to call other constructor in this class, this statement must be the first line of the constructor. Otherwise, a compiler error occurs.

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.