make method java code example

Example 1: java method

public class Main {
  public static void main(String args[]) {
    SayHi();
    
    int sum = AddNums(5, 6);
    System.out.println(sum);
  }
  
  public static void SayHi() { //This method has no return value
    System.out.println("Hi!");
  }
  
  public static int AddNums(int a, int b) { //This method has a return value
    return a + b;
}

Example 2: what are method in java

Static methods: A static method is a method that can be called and executed without creating an object
Instance methods: These methods act upon the instance variables of a class
Factory methods: A factory method is a method that returns an object to the class to which it belong

Example 3: make a commet in java

// Demo types of comments

/* This shows
   that the comment
   can be muilti line */
System.out.println("Hello"); // Inline comment
// This is a single line comment

Tags: