What is Method?
Reusable block of code or instruction to perform a specific task with their name, when is called.
Methods allow you to write code once and reuse it across your application, making your program modular, readable, and easy to maintain.
Structure of Method in java
The method declaration consists of six components,
Modifier returnType methodName(parameterList){ // method code (line of code to execute) return value ; (optional , based on returnType) } - Modifier --->Define the visibility or access type(eg.public private, protected.
- Return type ---> The data type of the value(int , string) **returned by the method, or **void if the method does not return a value.
- Method Name ---> The unique identifier used to call the method, typically returned in camelCase , the method name must be in verb as lowercase.
- ParameterList ---> Input value passed into method while calling it will be received by parameterlist().
- Method code ---> Actual code statement what the method to do , enclosed with curly braces{}.
1)Simple Method - No Return type and No Parameter,
public class Greeting{ public static void main(String[] args){ Greeting Mygreet = new Greeting(); Mygreet.greet(); } public void greet(){ System.out.println("Welcome"); } } - void --> There is no need for return value we use void.
2) A method with return type and parameter,
public class Calculator{ public static void main(String[] args){ Calculator myCal = new Calculator(); int result = myCal.add(10,20); system.out.println("Addition :" result); } public int add(int no1 , int no2){ return no1 + no2 ; } } Here the return type is int because the return value is int , if the return value is String the return type must be string.
Method overloading
same method name with different arugments or different type(data type) of arguments .
other name of method overloading is compile time polymorphism.
public class Calculator{ public static void main(String[] args){ Calculator myCal = new Calculator(); myCal.add(10,20); myCal.add(10,30,40); myCal.add(10); myCal.add(); } public int add(int no1 , int no2){ System.out,println(no1 + no2); } public int add(int no1 , int no2 , int no3){ System.out,println(no1 + no2 + no3); } public int add(int no1){ System.out,println(no1); } public int add(){ System.out,println(no arguments); } what is constructor ?
- A Special block of code that automatically executes when the object is created.
- It initially asign a value to the non-static variable.
- Called automatically when object is created.
- Initializing object specific values
Rule of creating constructor:
- same name --> constructor name must be in match the class name.
- No Return type ---> It can't have return type (even void) .if we add any return type java will treats as regular function.
- There is an default constructor with no arguments if we modified the default constructor with arugments were we want to create a new contructor for no arguments.
Types of constructor;
1) Default Constructor
- Defined by java complier if you create an empty object with no arguments.
public class Car{ public Car(){ # default constructor hide by java } public static void main(String[] args){ Car myCar1 = new Car(); } } 2) parameterized Constructor
- Accepts arguments to dynamically initialize an object with distinct data at the moment it is created.
public class Car{ String brand ; int model; public Car(String brand , int model){ this.brand = brand; this.model = model; } public static void main(String[] args){ Car myCar1 = new Car("BMW", 2026); } } 3) No-Argument constructor
- Written explicitly by the programmer to assign custom default values, taking zero parameters.because once we modified an default constructor with parameters the default constructor is invalid.
public class Car{ String brand ; int model; public Car(String brand , int model){ this.brand = brand; this.model = model; } public car(){ #for no arguments } public static void main(String[] args){ Car myCar1 = new Car("BMW", 2026); car myCar3 = new Car(); } } Constructor overloading ;
- Define multiple constructors in a single class as long as they have different parameter lists (different types, numbers, or order of arguments).
public class Car{ String brand ; int model; public Car(String brand , int model){ this.brand = brand; this.model = model; } public Car(int model ,String brand){ this.brand = brand; this.model = model; } public car(){ #for no arguments } public static void main(String[] args){ Car myCar1 = new Car("BMW", 2026); Car myCar2 = new Car(2026 , "Bmw"); car myCar3 = new Car(); } } 
