Class Methods In Java
Java class methods are discussed here with the goal of meeting educational needs.
Java Class Methods
From the Java Methods chapter, you learned that Java class methods perform certain actions by declaring methods within the class.
In the Main class, create a method named first_Method():
Example: 
Another Example: 
Upon calling first_Method(), a text (the action) is printed.
A method is called by writing its name followed by two parentheses () and a semicolon;.
Call java_Method() within Main class:
Example: 
call sum_method inside Main class:
Example: 
Public vs Static
You will usually find Java programs with static or public attributes and methods when we discuss Java class methods.
As shown above, we created a static method, which can be accessed without creating an object of the class, in contrast to public, which can only be accessed by objects.
To illustrate the difference between static and public methods, here is an example:
Example: 
Another Example: 
Remember: In the Java Modifiers chapter, you will learn more about these keywords (called modifiers).
Access Methods And Object
You will need to make a Calculator object called object_calculator.
Run the program using the percentage_method() and modulus_method() methods on the object_calculator object:
Example: 
Example Explaination:
- As a first step, we have created a new Main class with the class keyword.
- In the Main class, we make the percentage_calculator() and modulus_calculator() methods.
- When called, the percentage_calculator() and modulus_calculator() methods will print some text.
- There are two int parameters that modulus_calculator() accepts – mrx and ample – which we will apply in step 8).
- A Main class object is required in order to access the Main class and its methods.
- Next, run your program using the main() method, which is a Java built-in method (any code in main is executed).
- We created an object named calculator_object by using the new keyword.
- Lastly, we call the calculator_object methods percentage_calculator() and modulus_calculator() and run the program by specifying the object name (calculator_object), followed by a dot, then the method name (percentage_calculator() and modulus_calculator(19,4);).
You can see that both int parameters (19, 4) have been added to the modulus_calculator() method.
Another Example: 
You need to remember that.
Object attributes and methods can be accessed using the dot (.) in Java class methods.
A Java method is called by writing the name of the method followed by parentheses (), followed by a semicolon (;).
There must be a matching filename for a class (Main and Main.java).
Using Multiple Classes
It is good practice to make a class object in a Java class methods and then access it in a different class in java classes methods.
Make sure the java file name matches the class name. The following two files have been created in the same directory:
- First.java
- Last.java
First.java
public class First { public void print_My_Age(int mrx_age) { System.out.println("My age is: "+mrx_age); } public void print_ratio() { int mrx=16; int ample=5; float ratio=(float) mrx/ample; System.out.println("Result of 16 : 5 = "+ratio); } }
Last.java
public class Last { public static void main(String[] args) { First my_first_object=new First(); my_first_object.print_My_Age(21); my_first_object.print_ratio(); } }
After compiling both files:
C:UsersYour Name>javac First.java
C:UsersYour Name>javac Last.java
Run Last.java as follows:
C:UsersYour Name>java Last
As a result, we will get:
My age is: 21
Result of 16 : 5 = 3.2