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: 

public class Main { static void first_Method() { System.out.println("This my java method"); } }

Another Example: 

public class Main { static void second_Method() { System.out.println("This my another java method !!"); } }

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: 

public class Main { static void java_Method() { System.out.println("Welcome to Java Class method "); }public static void main(String[] args) { java_Method(); } // Outputs = Welcome to Java Class method }

call sum_method inside Main class:

Example: 

public class Main { static void sum_Method() { int mrx=24; int ample=16;System.out.println("Sum : "+(mrx+ample)); }public static void main(String[] args) { sum_Method(); } // Outputs = Sum : 40 }


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: 

public class Main { // Static method static void static_Method() { System.out.println("This is a static method which does not require an object to be accessed"); }// Public method public void public_Method() { System.out.println("This is a public method which will only be accessed by the means of an object"); }// Main method public static void main(String[] args) { static_Method(); // static method is called here// public_Method(); An error will be generated as it lacks object hereMain object = new Main(); // An object of Main class is created here object.public_Method(); // The public method calling is now possible because of the existance of an object }// Output 1: This is a static method which does not require an object to be accessed // Output 2: This is a public method which will only be accessed by the means of an object }

 

Another Example: 

public class Main {static void product_of_7_natural_numbers(){int mrx_product_of_7_nums=1 * 2 * 3 * 4 * 5 * 6 * 7;System.out.println("Product: "+mrx_product_of_7_nums);}public void sum_of_floats(){float mrx=15.14f; float ample=81.78f;System.out.println("Sum of floats: "+(mrx+ample)); } public static void main(String[] args) {product_of_7_natural_numbers(); // Static Method CallingMain my_object=new Main(); my_object.sum_of_floats(); // Public method accessed by using object} // Output1: Product: 5040 // Output2: Sum of floats: 96.92 }

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: 

// Create a Main class public class Main {// Create a percentage_calculator() method public void percentage_calculator(){ int obtained_marks=761; int total_marks=900; float formula = ((float)(obtained_marks*100)/total_marks);System.out.println("Percentage: "+formula+" %"); }// Create modulus_method and add two parameters public void modulus_method(int mrx,int ample) { System.out.println("Modulus value is: "+(mrx%ample)); }// Calling the methods of calculator inside the main method public static void main(String[] args) { Main calculator_object = new Main(); // Object of Calculator is created calculator_object.percentage_calculator(); // The public percentage_calculator() method is called here calculator_object.modulus_method(19,4); // The public modulus_calculator method is called here } // Output 1: Percentage: 84.55556 % // Output 2: Modulus value is: 3 }

Example Explaination:

  1. As a first step, we have created a new Main class with the class keyword.
  2. In the Main class, we make the percentage_calculator() and modulus_calculator() methods.
  3. When called, the percentage_calculator() and modulus_calculator() methods will print some text.
  4. There are two int parameters that modulus_calculator() accepts – mrx and ample – which we will apply in step 8).
  5. A Main class object is required in order to access the Main class and its methods.
  6. Next, run your program using the main() method, which is a Java built-in method (any code in main is executed).
  7. We created an object named calculator_object by using the new keyword.
  8. 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: 

// Create a Main class public class Main {// Assign a variable named mrx_name to the print_name() method public void print_name(String mrx_name){ System.out.println("The user name is : "+mrx_name); }// Age checking method is created below:public void check_age(int age){if(age<10){ System.out.println("Sorry! you are not Eligible for the competition"); } else{ System.out.println("Congratulations! You are eligible for this competition "); } }// Calling the methods inside the main method public static void main(String[] args) { Main my_object = new Main(); // Here, an object is created my_object.print_name("Albert"); // This lines includes the calling of print_name () method my_object.check_age(9); // The age_check() method is invoked here } // Output 1: The user name is : Albert // Output 2: Sorry! you are not Eligible for the competition }

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

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *