Methods in Java

Source: Internet
Author: User

1, how to create a method. (must be declared in a class and cannot be declared in a method.) )
A, modifier, if omitted, is automatically configured as default
b, static: optional, if static modification, then this method is a static method.
C, return type: Required, if the return type is null: void
D, method name: required, usually camel-named method
E, method parameters: required, the parentheses can be empty, and no arguments are passed in empty.
F, curly braces: required, double curly braces Identify the method body of the current method.
2, the method of parameter delivery:
A, value passing: A value is passed to the formal parameter and saved in the new memory address.
B, reference passing: A memory address is passed, once the value of a memory address is modified, then the corresponding will also modify the value of the same reference outside the method.
3. Overloading of methods
The subclasses replicate the methods in the parent class, and the content that is replicated in the subclass is executed directly.
The same method names, different parameter types, and return values can exist at the same time and are also a manifestation of polymorphism.
4. Public: Common, variable or method modified with this modifier, available for all class access
Private: A variable or method that is decorated with this modifier and can only be used for the content access of the current class.
Protected: Protected, this modifier modifies a variable or method that can be used only for the current class, as well as subclasses, and class content within the same package to access.


5. Replication law of subclasses and parent methods
A, the method in the parent class is a quilt class, and when the instantiation object of the subclass is called,
Methods in the parent class will no longer be executed. Unless you call super manually.
B, subclasses can directly use methods and variables that are not decorated by the private modifier in the parent class.
C, if the modifier of the parent class's method is private, then the subclass will not be able to replicate or be called directly.
However, you can declare a new method in a subclass. This method is independent of the subclass.

Methods in Java

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.