Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Static And Public Methods

public class Main {
  // Static method
  static void myStaticMethod() {
    System.out.println("You can call static methods without creating objects. ");
  }

  // Public method
  public void myPublicMethod() {
    System.out.println("You must call public methods by creating an object first.");
  }

  // Main method
  public static void main(String[] args) {
    myStaticMethod(); // Call the static method
    // myPublicMethod(); This would compile an error

    Main myObj = new Main(); // Create an object of Main
    myObj.myPublicMethod(); // Call the public method on the object
  }
}
 
PREVIOUS NEXT
Tagged: #Static #And #Public #Methods
ADD COMMENT
Topic
Name
2+7 =