Java Methods
Java 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.
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
-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
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("--------------------");
}
}
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.
}
Note: execution will always start from main method
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
-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
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);
}
}
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);
}
}