0% found this document useful (0 votes)
4 views

Java Methods

The document provides an overview of methods in Java, including their syntax, usage, and importance in organizing code. It explains method headers, signatures, implementation, and the concept of method overloading, along with examples of how to define and call methods with various arguments. Additionally, it discusses local and global variables, return types, and the significance of reusability in programming.

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Methods

The document provides an overview of methods in Java, including their syntax, usage, and importance in organizing code. It explains method headers, signatures, implementation, and the concept of method overloading, along with examples of how to define and call methods with various arguments. Additionally, it discusses local and global variables, return types, and the significance of reusability in programming.

Uploaded by

lsrinivas.rpa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Methods:

- A set of java statements which is enclosed within curly braces having a declaration and which
gets executed whenever we call it is called as method.

Syntax: AccessModifier NonAccessModifier returntype Methodname(Arguments)


{
//set of statements//
}
AccessModifier NonAccessModifier returntype-------->Method Header
Methodname(Arguments)--------->Method signature
{
//set of statements//-------->Method implementation or method body

Use of methods
--------------------
- It is not recommended to write a business logic directly in class.
- So java says for such situation make use of methods.

For Ex:
(Not Recommended)
class ATM
{
System.out.println("withdrawl money");
}
(Recommended)
class ATM
{
public static void Withdrawl()
{
System.out.println("enter amount to withdrawl");
}
}

Important Points
---------------------
1) Methods are used to perform some specific task.
2) Method will be consisting of method header and method signature.
3) Method should be declared within scope of class.
4) Within one class any number of methods we can write.
5) A method will be executed only when we call it.
6) we can call a method with help of method signature.
7) A method can be called any number of times depending on our requirement.
8) One method cannot be written in another method, it can only be called.

Ex1:
{
public static void fly()
public static void air()
{
}
}
-Above example is invalid

Ex2: public static void fly()


{
air();
}
public static void air()
{}
-Above is valid example
- The main advantage of using method is reusability, i.e once we created method, how many times
we want we can execute it.

public class Bank


{
public static void main(String args[])
{
System.out.println("Here we will show Customer's data");
custDetails(); //call to custDetails()(go and execute custDetails method)
System.out.println("---Showdetails again----");
custDetails();
}

METHODS WITH SOME INPUT


------------------------------------------
-A method can have any number of inputs in the form of arguments.
-Ex: public static void add() //method with zero argument or 0 input
public static void add(int i) //method with integer argument

-When we call any method with argument we have to pass that particular type of values.
-While passing arguments we have to make sure it will be as per sequence define in method
declaration.

Example
-----------
public class Login
{
public static void main(String args[])
{
registration("John", "john123@gmail.com", 9845915746l);
}
//static method without specific returntype and with arguments
public static void registration(String name, String email, long contact)
{
System.out.println("Name :"+name);
System.out.println("Email address:"+email);
System.out.println("Contact details: "+contact);
}
}

Practise Programs

1.WAP to create a facebook login app with args as username, password


public class FacebookLogin
{
public static void main(String args[])
{
login("Sreeni","r@Vi3421");
}
public static void login(String username, String password)
{
System.out.println("username : "+username);
System.out.println("password : "+password);
}
}

2.WAP to create a scholarship login app with args as halticket, dateofbirth, semester should print
above info for 3 people.
public class ScholarshipLogin
{
public static void main(String args[])
{
login("Sreeni","10-08-1993",7);
login("Kiran","25-07-1994",5);
login("Prasanna","16-05-1995",3);
}
public static void login(String hallticket,String DOB,int semester)
{
System.out.println("Hallticket :"+hallticket);
System.out.println("D.O.B :"+DOB);
System.out.println("Semester: "+semester);
System.out.println("--------------------");
}
}

METHOD WITH RETURN TYPE

- If we want we can define a method with any specific return type.


for ex: public static int add()
{
//perform operation whatever we want//
return integer_type_value;
}
-Whenever we define a method with return type, we are telling that my method is going to return
that particular type of data (value).
-It is mandatory to write return statement if we have a method with specific return type.

Return keyword

-It is used inside a method whenever we define a method with specific return type
-return keyword is used to take data and exit from method
-return keyword must be last statement in a method
-return keyword will not print the data.
-we cannot write more than one return statement in a method because after one return statement it
exits from the method, so that the next return statement cannot be executed.

Q. what is void?
A. no specific return type or method is not writing any specific type of value.

public class IntRegistration


{
public static long phnofield()
{
return 9874561337l;
}
public static char genderfield()
{
return 'M';
}
public static void main(String args[])
{
System.out.println(phnofield());
System.out.println(genderfield());
}
}

1.Create an APP which includes the method as


public class AppLogin
{
public static boolean login(String username, int pwd)
{
If (username=="MyApp" && pwd==1234)
{
return true;
}
else
{
return false;
}}
public static void main(String args[])
{
System.out.println(login("MyApp", 5454));
System.out.println(login("MyApp", 5354));
}
}

}
Note: execution will always start from main method

2. WAP for area of triangle, rectangle and square


class Areas
{
public static float areaOfTri(int breadth, int height)
{
float area1 = 0.5f * breadth * height;
return area1;
}
public static float areaOfCircle(int radius)
{
float pie=3.141f;
float area2 = pie * radius * radius;
return area2;
}
public static int areaOfRect(int breadth, int length)
{
int area3 = length * breadth;
return area3;
}
public static int areaOfSquare(int side)
{
int area4 = side * side;
return area4;
}
public static void main(String args[])
{
System.out.println("Areaof Triangle is : "+ areaOfTri(5,10));
System.out.println("Areaof Circlee is : "+areaOfCircle(5));
System.out.println("Areaof Rectangle is : "+areaOfRect(3,8));
System.out.println("Areaof Square is : "+areaOfSquare(4));
}
}

Local variable Global variable


Variables which are declared outside
Variables which are declared inside the the method and within the scope of
method and within the scope of class. class.
Local variables are accessible only within
the method in which they are declared,
they are not accessible outside of that Global variables are accessible
method. everywhere within the scope of a class.
It is mandatory to initialize local variables, if It is not mandatory to initialize, if we
we did not initialize we will get compile did not initialize, JVM will take default
time error. values based on data types.
Global variables are divided into 2
There are no types for local variables. types i.e. static and non-static.
Global variables can be public, private,
Local variables can be default or final. protected, default or final.
Global variables are present in Heap
Local variables are present in Stack area. area.

METHOD OVERLOADING
-------------------------------------

Developing multiple methods with in the same class and variation in argument list. Variation in
argument list means
- Variation in the datatype of the argument
- Variation in the length of the argument
- Variation in the order of the occurrence of the argument
Is called as method Overloading

RULES FOR DEFINING ARGUMENT LIST


-----------------------------------------------------------
1. Number of arguments must be different.
Ex:
swiggy()
swiggy(String name)
swiggy(String name, int orderid)

2. Types(datatype) of arguments must be different.


Ex:
add(int i, int j)
add(double d, double d1)
add(String s1, String s2)

3. Sequence or position of Arguments must be different.


Ex:
login(String ul, int id)
login(int id, String pwd)

-We go for method overloading when we want to perform one task in multiple ways.

Examples
---------------
a)Travelling(hyd-->bang)
--------------------------------
-bus
-train
-aeroplane
-bike
-car......etc
b)keeping phone password
---------------------------------
-Nmberlock
-Drawing pattern
-Finger print
-Face print
-Face sensor

c)payment in any Ecommerce


-------------------------------------
-Credit
-Debit
-Wallets(Gpay,Phonepe,Amazonpay,Paytm)
-Cash on Delivery

d)Banking
-------------
-Online banking
-Physical baning
-ATM banking
-App banking

Eg:
class Mover
{
public static void main(String args[])
{
add();
add(100,200);
add('A','B');//A=65,B=66
add(100,"java");
add("SQL",200);
}
public static void add()
{
System.out.println(10+20);
}
public static void add(int i,int j)
{
System.out.println(i+j);
}
public static void add(char ch1,char ch2)
{
System.out.println(ch1+ch2);
}
public static void add(int i,String s)
{
System.out.println(i+s);
}
public static void add(String s,int i)
{
System.out.println(s+i);
}
}

‐WAP to develop the APP for Payment Module using Method Overloading.
class Payment
{
public static void payment(String wallettype, int UID)
{
System.out.println("WalletType : "+wallettype);
System.out.println("UID : "+UID);
}
public static void payment(String cardtype, long cardno, int cvvnumber)
{
System.out.println("CardType : "+cardtype);
System.out.println("CardNo : "+cardno);
System.out.println("CvvNumber : "+cvvnumber);
}
public static void payment(String type,String username,int pwd,long
Accountnumber)
{
System.out.println("Type : "+type);
System.out.println("Username : "+username);
System.out.println("Pwd : "+pwd);
System.out.println("AccountNumber : "+Accountnumber);
}
public static void main(String args[])
{
payment("Gpay",9573);
payment("Debitcard",4581643278941254l,522);
payment("savings","Sreenivas",6547523,35486785214l);
}
}

Q. Can we overload main() or not ?


A. Yes, we can overload main() because JVM knows a main() which is having
arguments as String args[](command line arguments), so if we define any
other methods whose name is main it will be considered as user define
methods.

Note: In practical or real-time it is suggested not to overload main().

class Mover1
{
public static void main(String args[])
{
main();
main(100,200);
main('A','B');//A=65,B=66
main(100,"java");
main("SQL",200);
}
public static void main()
{
System.out.println(10+20);
}
public static void main(int i,int j)
{
System.out.println(i+j);
}
public static void main(char ch1,char ch2)
{
System.out.println(ch1+ch2);
}
public static void main(int i,String s)
{
System.out.println(i+s);
}
public static void main(String s,int i)
{
System.out.println(s+i);
}
}

Q. Can we overload final methods ?


A. Yes, we can overload final methods because final keyword says do not change implementation and
in overloading we are not changing implementation rather we are changing arguments.

Q. Can we overload private methods or not ?


A. Yes, we can overload private methods because private methods are accessible everywhere in
same class and overloading also happens within class.

Q. Can we overload non static methods or not ?


A. Yes, we can overload non static methods but to call them we have to create an Object.

You might also like