Method

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Class and objects 2.

sqrt() : It is a method of Math class which is present


in package java.lang. It calculates the square root of a
number.
Class public class SquareRoot
{
A method is a collection of statements that are grouped
public static void main(String[] args)
together to perform an operation.
{
Methods are also known as functions or procedures in
System.out.println("Square root of 16: "
other programming languages.
+Math.sqrt(16));
Advantage of Method
}
 Code Reusability
}
 Code Optimization
Output:
Square root of 16: 4.0
Syntax of a method in Java:
Modifier returnType methodName ( arg1,arg2.. argn ) public class MaxValue
{ {
// Method body public static void main(String[] args)
// Statements to perform the operation {
// Optionally, a return statement to return a value int num1 = 20, num2 = 50, maxValue;
} maxValue = Math.max(num1,num2);
System.out.println("Max value: " +maxValue);
}
}
Output:
Max value: 50
2. User-defined Method
The method written by the user or programmer is
known as a user-defined method.
Types of Methods in Java
We can declare two types of user-defined methods in a
program.
1. Instance Method
2. Static Method
Instance Method:
 An instance method is used to implement
behaviors of each object/instance of the class.
Therefore, instance method is linked with an
1. Predefined methods: object.
In Java are those methods that are already defined in  They can access instance variables and other
the Java API (Application Programming Interface) to use instance methods directly.
in an application.  An instance method is also known as non-static
method. So, without creating an object of the
Look at some examples below: class, the methods cannot exist to perform their
1. print(): It is a predefined method present in the desired behaviors or task. It is allocated in the
package java.io.PrintSteam. The print(“….”) prints the heap memory during the object creation.
string inside the quotation marks.
Syntax of Instance Method: 2.Static Methods:
public class ClassName {  These methods belong to the class itself rather
// Instance variables (attributes) than instances of the class.
 They can access only static variables and other
// Constructor(s) static methods directly.
 Static methods load into the memory during
// Instance method class loading and before object creation.
accessModifier returnType
methodName(parameterType parameter1, Syntax
parameterType parameter2, ...) { public class ClassName {
// Method body // Static method
// Statements to perform the operation static accessModifier returnType
// Optionally, a return statement to return methodName(parameterType parameter1,
a value of returnType parameterType parameter2, ...)
} {
} // Method body
Calling an Instance Method: // Statements to perform the operation
There are two steps to call an instance method in java. // Optionally, a return statement to return a value of
1. First, create an object of a class before calling an returnType
instance method of that class. }
2. Second, call the method with a reference variable }
using the dot operator. Calling Static Method in Java
Syntax: To call a static method, we use dot operator with the
classname obj=new classname(); class name.
obj.instance_method_name(actual_parameters); Syntax:
class_name.method_name(actual_parameters);
Example: Example:
public class Calculator { public class Square
// Instance method to add two numbers {
public int add(int num1, int num2) // Static method to calculate the square of a number
{ public static int square(int num)
return num1 + num2; {
} return num * num;
public static void main(String[] args) }
{ }
// Create an object of the Calculator class Public class Main
Calculator calc = new Calculator(); {
public static void main(String[] args)
// Call the add method using the calc object {
int result = calc.add(5, 3); int squareResult =Square.square(5);
// Print the result System.out.println("Square result: " +
System.out.println("Result: " + result); squareResult);
}
} }
Output: }
Result : 8 Output: squareResult:25

You might also like