Utilizor
Contact Us

Java Class Methods

Methods belonging to a class.

Examples

Static Method

Calling a static method directly.

public class Main {
  static void myStaticMethod() {
    System.out.println("Static methods can be called without creating objects");
  }

  public static void main(String[] args) {
    myStaticMethod(); // Call the static method
  }
}

Public Method

Calling a public method via an object.

public class Main {
  public void myPublicMethod() {
    System.out.println("Public methods must be called by creating objects");
  }

  public static void main(String[] args) {
    Main myObj = new Main(); // Create an object of Main
    myObj.myPublicMethod(); // Call the public method
  }
}

Test Your Knowledge

Java Quiz

No quiz available for this topic yet.