Java object-oriented programming--the behavior of the fifth chapter object

Source: Internet
Author: User
Tags call by reference modifiers naming convention

5.1 Method Call Stack

When a method is called, it causes the control flow to jump to the called method. Then, control the statements in the process execution method. Of course, the method being executed may invoke other methods, causing the control flow to jump to other methods. All method invocations are maintained in a structure called the call stack. The currently executing method is at the top of the call stack, and after the current method executes, it is removed from the top of the call stack, which controls the previous method in the process return stack. When a new method is called, the new method is placed at the top of the call stack.

In a Java program, the first method called is main (), which is called by the JVM. So the main () method is always at the bottom of the call stack.

5.2 Calling methods

When a method is called, the method is placed at the top of the call stack until the method execution is complete. When a method is executing, there are three scenarios:

    • method returns a value. In this case, a base type or reference type is passed back to the caller of the method.
    • method does not return a value. In this case, the return value is declared void.
    • Method throws an exception to the caller of the method.

5.3 Method signature

With the method signature, we can see everything we need to know to invoke a method. The signature of a method includes information such as the method name, the parameter list, the data type of the return value, and so on.

In the order in which the methods are declared, each part of the method signature is listed as follows:

    • The access modifier. Possible values for the access modifier include public, private, protected, or the default access modifier (that is, there is no access modifier). The public access modifier allows the method to be called from anywhere. The private access modifier means that no one can call it except inside the class. The protected access modifier and the default access modifier apply to inheritance and packages, respectively.
    • Optional modifiers. The next part of the method signature is an optional modifier, including static, Fina, abstract, native, and synchronized. The methods of the class may not use these optional modifiers, or you can use more than one modifier.

* The order of the access modifier and optional modifiers is arbitrary. But Java programmers are accustomed to placing access modifiers in front of an optional qualifier.

    • The return value. The method signature must include the type of the return value. If the method does not need to return a value, void is used, otherwise the data type of the return value is specified.
    • The method name. The method name must appear after the return value. The method name can be any valid Java identifier. The Java naming convention requires that the method be a mixed-case camel, where the first letter of the first word of the method name is lowercase and the first letter of the other word is uppercase. such as: ToString, GetDay.
    • Formal parameter list. The method name must be followed by a form parameter list enclosed in parentheses. When the method is called, the data can be passed in by the caller of the method. The data passed in is copied to the formal parameter, and a formal parameter consists of a data type and an identifier.
    • Throws a list of exceptions. The method can throw an exception to the caller of the method.

5.4 Formal parameters and actual parameters

The signature of a method includes a list of formal parameters (Parameter), which is used to declare the type of data passed to the method. The data passed to the formal parameter is called the actual parameter (arguement). When a method is called, the actual argument must be passed to each formal parameter in the formal argument list.

5.5 Call by value

When the actual parameter is passed to the formal parameter, the data of the actual parameter is copied to the formal parameter. In programming, the process of copying data between method invocations is called call by value.

In Java, we do not need to specify the actual arguments to be passed using the call by value, because it happens automatically and is actually the only option. Other programming languages may be invoked by reference or by pointer, in which case the actual parameter is not copied to the formal parameter. In Java, you cannot call by reference or by pointer. Regardless of the type of actual argument passed to the method, the associated formal parameter gets a copy of that data, which is how the call to value works.

5.6 Method Overloading

Java allows methods to be overloaded. Method overloads are used when a class has two or more methods with the same name but has a different argument list. Multiple methods with the same name may seem unnecessary, but the method overloads are used frequently in Java and other programming languages.

5.7 Constructors

A constructor is a special method in a class that is called when an object is instantiated. The purpose of the constructor is to initialize the member variables in the object when the object is instantiated.

A constructor differs from a method in that the constructor must meet the following two properties:

    • The name of the constructor must be the same as the class name;
    • The constructor cannot declare the return value, nor can it return void.

When multiple constructors are added for a class, the rules for method overloading are applied. Each constructor must have a unique formal parameter list to distinguish it from other constructors.

When you create a new object, Object Nn=new object (), where parentheses are called constructors.

If we write a class, but no constructors are added to the class, the compiler adds a default parameterless constructor to the class. When an object is instantiated using the keyword new, the constructor is called. A class can have more than one constructor, in which case the constructor to invoke depends on the actual arguments used by the new operator.

To avoid duplication of code, we can call a specific constructor with all constructors, and let this particular constructor do the work of repeating the code, and in the constructor, you can use the keyword this to invoke other constructors in the same class. If a constructor invokes the other constructors in this class with the This keyword, the This statement must be the first row of this constructor, or a compilation error will occur.

In Java, the This keyword has two different uses, and the This keyword used within the constructor is different from the This reference, which represents each object itself.

Java object-oriented programming--the behavior of the fifth chapter object

Related Article

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.