Java Class Methods
You learned from the Java Methods chapter that methods are declared within a class, 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 a text (the action) when it is called. To call a method, write the method's name followed by two parentheses () and a semicolon ;.
Example: Calling myMethod() Inside main
public class Main {
static void myMethod() {
System.out.println("Hello World!");
}
public static void main(String[] args) {
myMethod();
}
}Output:
Hello World!Static vs. Public Methods
You will often see Java programs that have either static or public attributes and methods.
In the example above, we created a static method, which means that it can be accessed without creating an object of the class, unlike public, which can only be accessed by objects.
Example: Demonstrating 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: Creating and Calling Methods on an Object
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
}
}Output:
The car is going as fast as it can!
Max speed is: 200Explanation:
- We created a custom
Mainclass with theclasskeyword. - We created the
fullThrottle()andspeed()methods in theMainclass. - The
fullThrottle()andspeed()methods print out some text when called. - The
speed()method accepts anintparameter calledmaxSpeed(used in step 8). - To use the
Mainclass and its methods, we need to create an object of theMainclass. - In the
main()method, which is a built-in Java method, we execute our program. - Using the
newkeyword, we created an object namedmyCar. - We then call the
fullThrottle()andspeed(200)methods on themyCarobject.
Using Multiple Classes
Like we specified in the Classes chapter, it is a good practice to create an object of a class and access it in another class.
Example: Using Multiple Classes
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
}
}Compiling and Running
When both files have been compiled:
C:\Users\Your Name>javac Main.java
C:\Users\Your Name>javac Second.javaRun the Second.java file:
C:\Users\Your Name>java SecondOutput:
The car is going as fast as it can!
Max speed is: 200Key Takeaways
- The dot (
.) is used to access the object's attributes and methods. - To call a method in Java, write the method name followed by parentheses
(), then a semicolon;. - A class must have a matching filename (e.g.,
MainandMain.java).