Methods in Java

Source: Internet
Author: User

Methods in Java

Java, as an object-oriented language, has the following characteristics: inheritance, encapsulation, polymorphism, abstraction, classes, objects, instances, methods, and overloads.

The concept of Java methods is explained today.

What is a method?

A Java method is a collection of statements that perform a function together.

method is an orderly combination of steps to solve a class of problems

Method is included in the class or object

Method is created in the program and is called in other places

Advantages of the method

1. Make the program shorter and clearer

2, in favor of the maintenance of the program

3, can improve the development efficiency of the program

4, improve the reusability of the Code

Naming rules for methods

1. Must start with a letter (A-Z or a-Z), ' _ ' or ' $ '

2, can include the number, but cannot start with it

Syntax for method definitions
Modifier returns a value type method name (parameter type argument name) {        ....        Method Body        ....        return value;            } public int test (int a) {        int b=10;        if (a>b) {             renturn A;       } else {             return b;       }}                

Sometimes there can be no return type (judging if a is greater than 100)

public void Test (int a) {int b=100;if (a<=b) {/*system is a system class, out is a standard output stream object, println () is a method */system.out.println (" A is less than or equal to 100 ");} ELSE{SYSTEM.OUT.PRINTLN ("A greater than 100");}}

The method contains a method header and a method body. Here are all the sections:

modifier (modifier): This is optional and tells the compiler how to call the method. Defines the type of access for the method.

return value type : The method may return a value. Returnvaluetype is the data type of the method return value. Some methods perform the required operations, but do not return a value. In this case, Returnvaluetype is the keyword void.

Method Name: is the actual name of the method. The method name and the parameter table together form the method signature.

parameter type : The parameter is like a placeholder. When the method is called, the value is passed to the parameter. This value is called an argument or variable. The parameter list refers to the parameter type, order, and number of parameters of the method. The parameter is optional, and the method can contain no parameters.

method Body : The method body contains a specific statement that defines the function of the method.

Method composition

Instance:

1  Public classTest {2     /*This method also belongs to the static method*/3      Public Static voidMain (string[] args) {4     intA=1;5     intB=3;6Test t=NewTest ();//Initializes an instance of the class7K=t.sum (A, b);//called by an instance of the class8Sumab (A, b);//static methods can be called directly9     }Ten     /*This method is an instance method*/ One      Public  intSumintAintb) { A         returnA +b; -     } -     //The following is a static method (class method) and a call the sumab (A, b); -Test.sumab (A, b);//This method can be used to invoke methods of an external class -     Static voidSumab (intIintj) { -         intK=0; +k=i+J;  - System.out.println (k); +     } A}
View CodeConstruction Method:

The so-called construction method,
1, the method that is called by default when instantiating a new object using the keyword new;
2, the main work done by the construction method is to assign the initial value to the data member of the newly created object.
There are a few things to be aware of when using construction methods
1. The construction method name and the class name to which it belongs must be consistent;
2. The construction method does not return a value, nor can it use void;
3. Construction methods can also be overloaded as normal methods;
4. The construction method cannot be modified by static and final;
5. Construction methods cannot be inherited, subclasses use the Super keyword to construct a parent class

1  Public class Test {2       Public int Test (int A,int  b) {3         return a +b; 4      }; 5 }
View Code

Others will explain later

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.