Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Methods in Java

Uploaded by

susmithamadineni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Methods in Java

Uploaded by

susmithamadineni
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Methods in Java

 Methods in Java are the building blocks of a Java application. In Java, a method
is a set of code used to write the logic of the applications which perform some
specific tasks or operations.
 In Java programming, a method is executed when it is called from another
method. The main() method is the first method that is executed by JVM (Java
Virtual Machine) in Java program.
 When a method is called, it returns a value to the calling method. It can also
perform a task without returning any value to the calling method.
 We can call a method from anywhere in Java program. Each method has a single
entry point and a single exit. A method can also call itself. We know this
technique as a recursion in Java.
}
Method Declaration in Java
 A method must be declared inside the class.
 In general, a method has six fundamental parts such as modifiers, method name, return
type, parameter list, exception list, and body.
Method Signature in Java
The method name with parameter list (number of parameters, type of parameters, and order of
parameters ) is called method signature in Java.
Examples
1. add(int a, int b) // Number of parameters is 2, and type of parameter is int.
2. m1(String name) // Number of parameters is 1, and type of parameter is String.
3. sub(): No parameter.

Access Specifier:
Access specifier or modifier is the access type of the method. It specifies the visibility of the method.
Java provides four types of access specifier:
 Public: The method is accessible by all classes when we use public specifier in our application.
 Private: When we use a private access specifier, the method is accessible only in the classes in which it is
defined.
 Protected: When we use protected access specifier, the method is accessible within the same package or
subclasses in a different package.
 Default: When we do not use any access specifier in the method declaration, Java uses default access
specifier by default. It is visible only from the same package only.
Return Type:
Return type is a data type that the method returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return anything, we use void keyword.
Method Name:
It is a unique name that is used to define the name of a method. It must be corresponding to
the functionality of the method. Suppose, if we are creating a method for subtraction of two
numbers, the method name must be subtraction(). A method is invoked by its name.
Parameter List:
It is the list of parameters separated by a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method has no parameter, left the parentheses blank.
Method Body:
It is a part of the method declaration. It contains all the actions to be performed. It is
enclosed within the pair of curly braces.
Naming a Method:
While defining a method, remember that the method name must be a verb and start with
a lowercase letter.
Types of Method:
There are two types of methods in Java:
 Predefined Method
 User-defined Method
Predefined methods:
 Predefined methods in Java are those methods that are already defined in the Java API (Application
Programming Interface) to use in an application.
 It is also known as the standard library method or built-in method.
 We can directly use these methods just by calling them in the program at any point.
 Some pre-defined methods are length(), equals(), compareTo(), sqrt(), etc.
 Each and every predefined method is defined inside a class. Such as print() method is defined in
the java.io.PrintStream class.
Example:

public class PreDefinedMethod


{
public static void main(String[] args)
{
// using the max() method of Math class
System.out.print("The maximum number is: " + Math.max(9,7));
}
User-defined Method:
The method written by the user or programmer is known as a user-defined method. These
methods are modified according to the requirement.
Create a User-defined Method:
public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
Static Method:
 A method that has static keyword is known as static method.
 A method that belongs to a class rather than an instance of a class is known as a static
method.
 A static method is that we can call it without creating an object.

public class Display


{
public static void main(String[] args)
{
show();
}
static void show()
{
System.out.println("It is an example of static method.");
}
Instance Method:
 The method of the class is known as an instance method.
 It is a non-static method defined in the class.
 Before calling or invoking the instance method, it is necessary to create an object of its class.
public class InstanceMethodExample
{
public static void main(String [] args)
{
//Creating an object of the class
InstanceMethodExample obj = new InstanceMethodExample();
//invoking instance method
System.out.println("The sum is: "+obj.add(12, 13));
}
int s;
//user-defined method because we have not used static keyword
public int add(int a, int b)
{
s = a+b;
//returning the sum
return s;
}
Abstract Methods:
The method that does not has method body is known as abstract method.
To create an abstract method, we use the keyword abstract.
Syntax:
abstract void method_name();
 abstract class Demo //abstract class
{
//abstract method declaration
abstract void display();
}
public class MyClass extends Demo
{
//method impelmentation
void display()
{
System.out.println("Abstract method?");
}
public static void main(String args[])
{
//creating object of abstract class
Demo obj = new MyClass();
//invoking abstract method
obj.display();
}
Main Method in Java:
Call by Value and Call by Reference in Java
“Call by value” in Java means that argument’s value is copied and is passed to the parameter list of a method.
call by value (or pass by value) involves passing a copy of the actual value of a variable to a method. This copy can then be modified within the
method, but the original value remains unchanged outside the method. This mechanism prevents accidental alterations to the original data or
value.
package callbyValueExample;
public class CallbyValue
{
int change(int b)
{
++b; // Changes will be in the local variable only.
return b;
}
public static void main(String[] args)
{
// Create an object of class.
CallbyValue obj = new CallbyValue();
int a = 20;
int x = obj.change(a);

System.out.println("Value of a after passing: " +a);


System.out.println("Value of x after modifying: " +x);
}
}
Call by Reference (Pass by Reference) in Java
When we pass a variable of class type as an argument to a method, actually, a copy of
a reference (or memory address) to an object is passed by value to the method, not a
copy of the object itself.
 This is because Java does not copy the object into the memory. Actually, it copies
reference of the object into the memory and passes the copy to the parameters of
the called method.
 If we change the reference of the object, then the original reference does not get
changed because this reference is not original. It’s a copy.
class Operation2{
int data=50;

void change(Operation2 op){


op.data=op.data+100;//changes will be in the instance variable
}

public static void main(String args[]){


Operation2 op=new Operation2();

System.out.println("before change "+op.data);


op.change(op);//passing object
System.out.println("after change "+op.data);

}
}

You might also like