Java Class Methods


Java Class Methods

You learned in the Java Methods chapter that the methods are announced in the classroom, and that they are used to perform certain actions:


Example

Create a method named myMethod() in Main:

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

myMethod() prints the text (action), when it is called. To name a path, write the name of the path followed by two brackets () and a semicolon ;


Example

Inside main, call myMethod():

public class Main {
            static void myMethod() {
              System.out.println("Hello World!");
            }
          
            public static void main(String[] args) {
              myMethod();
            }
          }
          
          // Outputs "Hello World!"
          


Static vs. Non-Static

You will usually see Java applications with static or public symbols and methods.

In the example above, we have created a static method, which means that it can be accessed without creating a class object, unlike the public object, which can only be accessed with objects:


Example

An example to demonstrate the differences 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
            }
          }
          

Note: You will learn more about these keywords (called modifiers) in the Java Modifiers chapter.



Access Methods With an Object


Example

Create a Car object named myCar. Call the fullThrottle() and speed() methods on the myCar object, and run 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've created a custom Main class with the class keyword.
  2. We are building complete Trrottle () and speed () modes in the main class.
  3. The fullThrottle() method and the speed() method will print a specific text, if called.
  4. The speed() method accepts the int parameter called maxSpeed ​​- we will use this in 8).
  5. In order to use the Main class and its methods, we need to create a Main Class object.
  6. Then, go to the Main() method, which you now know is the Java built-in method that runs your program (any code within the main is used).
  7. Using a new keyword we have created an object named MyCar.
  8. Then, we call the fullTrottle() and speed() method of the myCar object, and then run the program using the object name (myCar), followed by a dot(.), Followed by the path name (fullThrottle(); and speed(200);). Note that we add a 200 int parameter within the speed() mode.

Remember that..

The dot (.) Is used to access the features and modes of the object

To drive a path in Java, type the path path followed by a parentheses (), followed by a semicolon (;).

The class should have the same filename (Main and Main.java).


Using Multiple Classes

As we said in the class chapter, it is a good practice to create something in a class and access it in another class.

Remember that the java file name must match the class name. In this example, we created two files 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
            }
          }
          

Once both files have been merged:

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

Launch the Second.java file:


C:\Users\Your Name>java Second

And the output will be:


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