Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Class Methods

Java Class Methods

From the Java Methods chapter, you discovered that methods are defined within a class and employed to execute specific actions

Example

Define a method called myMethod() in the Main class:

public class Main {
  static void myMethod() {
    System.out.println("Hello World!");
  }
}

When called, myMethod() prints a text (the action). To invoke a method, simply write the method’s name followed by two parentheses () and a semicolon;

Example

Within the main method, invoke myMethod():

public class Main {
  static void myMethod() {
    System.out.println("Hello World!");
  }

  public static void main(String[] args) {
    myMethod();
  }
}

// Outputs "Hello World!"

Static vs. Public

It’s common to encounter Java programs featuring either static or public attributes and methods.

In the example provided, we crafted a static method, indicating that it can be accessed without necessitating the creation of a class object. In contrast, public methods can only be accessed through objects.

Example

Here’s an example illustrating the distinctions between static and public methods:

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

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

  // 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
  }
}

 

Keep in mind: You’ll delve deeper into these keywords, referred to as modifiers, in the Java Modifiers chapter.

Access Methods With an Object

Example

Instantiate a Car object named myCar. Invoke the fullThrottle() and speed() methods on the myCar object, then execute the program:

// Create a Main class
public class Main {
 
  // Create a fullThrottle() method
  public void fullThrottle() {
    System.out.println("The car is going as fast as it can!");
  }

  // Create a speed() method and add a parameter
  public void speed(int maxSpeed) {
    System.out.println("Max speed is: " + maxSpeed);
  }

  // Inside main, call the methods on the myCar object
  public static void main(String[] args) {
    Main myCar = new Main();   // Create a myCar object
    myCar.fullThrottle();      // Call the fullThrottle() method
    myCar.speed(200);          // Call the speed() method
  }
}

// The car is going as fast as it can!
// Max speed is: 200

Example explained

  1. We defined a custom Main class using the class keyword.
  2. We implemented the fullThrottle() and speed() methods within the Main class.
  3. The fullThrottle() and speed() methods will display some text when invoked.
  4. The speed() method takes an int parameter named maxSpeed, which we will use in step 8).
  5. To utilize the Main class and its methods, we need to instantiate an object of the Main class.
  6. Next, navigate to the main() method, which, as you know, is a built-in Java method that executes your program (any code inside main is run).
  7. We used the new keyword to create an object named myCar.
  8. Next, we call the fullThrottle() and speed() methods on the myCar object by using the object’s name (myCar), followed by a dot (.), and then the method names (fullThrottle(); and speed(200);). Note that we pass an int parameter of 200 to the speed() method.

Remember that..

The dot ( ) operator is utilized for accessing an object’s attributes and methods.

To invoke a method in Java, specify the method name succeeded by a pair of parentheses ( ), and conclude with a semicolon ( ; ).

Ensure that a class corresponds with its filename (Main class with Main.java).

Using Multiple Classes

 As outlined in the Classes chapter, it’s advisable to instantiate an object of a class and access it in another class.

Keep in mind that the Java file name should match the class name. In this instance, two files have been generated in the same directory:

  • Main.java
  • Second.java

Main.java

public class Main {
  public void fullThrottle() {
    System.out.println("The car is going as fast as it can!");
  }

  public void speed(int maxSpeed) {
    System.out.println("Max speed is: " + maxSpeed);
  }
}

Second.java

class Second {
  public static void main(String[] args) {
    Main myCar = new Main();     // Create a myCar object
    myCar.fullThrottle();      // Call the fullThrottle() method
    myCar.speed(200);          // Call the speed() method
  }
}

After compiling both files:

C:\Users\Your Name>javac Main.java

C:\Users\Your Name>javac Second.java

Execute the Second.java file:

C:\Users\Your Name>java Second

The output displayed will be:

The car is going as fast as it can!

Max speed is: 200