A class method in java is a method that we called on the class itself. The class method is like the instance method of a class. But the only difference is that we can call the class method through its class name without creating any object of that class. A class method is associated with a class and not with an individual instance of the class. The class method is also known as the static method.
Example:
public class Myclass{
static void myMethod(){
System.out,.printIn("Java Class Method");
}
}
In the above example, the method name is myMethod(). myMethod() prints the text when we call it. To call the method write the method name followed by two parentheses () and a semicolon(;).
Example:
public classs Myclass{
static void myMethod(){
System.out.printIn("Java Class Method");
}
public static void main(String[] args){
myMethod();
}
}
As a Result:
Java Class Method
Access method with an object
Here is an example:
// 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
}
}
As a result:
The car is going as fast as it can!
Max speed is: 200
Example explained
Example explained
1) We created a custom Main class with the class keyword.
2) We created the fullThrottle() and speed() methods in the Main class.
3) The fullThrottle() method and the speed() method will print out some text when they call it.
4) The speed() method accepts an int parameter called maxSpeed – we will use this in 8).
5) To use the Main class and its methods. We need to create an object of the Main Class.
6) Then go to the main() method. In which you know by now is a built-in Java method that runs your program (any code inside main will execute).
7) By using the new keyword we created an object with the name myCar.
8) Then we call the fullThrottle() and speed() methods on the myCar object. And run the program using the name of the object (myCar), followed by a dot (.), followed by the name of the method (fullThrottle(); and speed(200);). Notice that we add an int parameter of 200 inside the speed() method.
Things to remember for class method in java.
The following points to remember while working with class methods.
- A class method should call using the class name rather than an object reference variable.
- A static method is good for a utility method that does not depend upon a particular instance variable value.
- You have a class with only static methods and you do not want the class to be, instantiated. You can mark the constructor private. For example: In the Math class, the constructor, marked private because it contains only static methods.
Recommended Post:
Convert java object into a JSON
Difference between the Java and JavaScript
How are java objects stored in memory?
Reference:
book// learn programming in java // Anshuman Sharma