In Java, the use of methods is in two steps:
1. Definition method
public void print () { System.out.println ("Hello world!");}
Attention:
(1) The method body is placed in a pair of curly braces to achieve a specific operation
(2) The method name is mainly used when calling this method, we should pay attention to the naming of the normalization, generally we take the first word of the initial letter lowercase, the other Word capitalized form.
2. Call method
We define the method to invoke it to play its part, and when we need to invoke the method to perform an operation, we can first create the object of the class and then pass the
The name of the object. Method Name (), to implement a call to the method. Here's a demonstration:
public class helloworld{public static void Main (string[] args) { //create object whose name is HW HelloWorld HW = new HelloWorld (); Call the method Hw.print () that we defined above, in the form of the object name, method name () ; } Define method public void print () { System.out.println ("Hello world!");} }
The result of the operation is: Hello world!
Java Learning Note 2