[Java] Create a java method [Note] [java11]

less than 1 minute read

class and main method

class

How to write a class.

setumeiclass.java


public class setumeiclass/*⇐file name*/{
// Write the main method in this.
}

main method

It can’t start without this. Write the code in this. (Basically.)

Test.java



public class Test{
  public static void main(String args[]){
/*Write the code here!!*/
  }
}

Example

rei.java



public class rei{
  public static void main(String args[]){
    System.out.println("Hello,World!!");
  }
}

Execution result:

Hello, World!!

Self-made method

zisaku


public class zisaku{
public static type name method (argument) {
Code executed by this method
}
public static void main(String args[]){
Use your own method here
}
 
}

myclass.java


public class myclass{
public static void main(String args[]){
myclass(1,2);
}


public static void myclass(int a,int b){
System.out.println(a+b);
}
}


Execution result:

3

Since the values of the arguments a and b of myclass are 1 and 2, a+b(1+2) is 3.