Types of Methods: ================= what is method? =============== ==> method is named block and member of the class. ==> used to define the functionality accessing of the data processing of the data returning of the data printing of the data ==> two types: 1) Instance Methods ==> can always perform the functionality based on the data of objects. package AIT.IO.Java; class Employee{ int eid; String ename; double salary; Employee(int eid,String ename,double salary) { this.eid = eid; this.ename = ename; this.salary = salary; } // Instance Method void displayData() { System.out.println("The Employee Details are = "); System.out.println("ID = "+eid); System.out.println("Name = "+ename); System.out.println("Salary = "+salary); } } public class Methods { public static void main(String[] args) { Employee emp1 = new Employee(111,"Rakesh",23000.0); emp1.displayData(); Employee emp2 = new Employee(1111,"Sony",25000.0); emp2.displayData(); } } 2) Static Methods ==> can always perform the functionality based on parameters/without the data of objects. ex: class TelecomOperations{ void callCostMethod(double duration) { return duration * 0.5; } } TelecomOperation obj = new TelecomOperation() obj.calCostMethod(600.0); package AIT.IO.Java; class StaticMethod{ int eid; String ename; double salary; StaticMethod(int eid,String ename,double salary) { this.eid = eid; this.ename = ename; this.salary = salary; } double salaryHikeCalculation(double salary) { return (salary * 1.5); } } public class MainClass { public static void main(String[] args) { StaticMethod sm1 = new StaticMethod(101,"Rajesh",23000.0); StaticMethod sm2 = new StaticMethod(201,"Reshma",21050.0); System.out.println(sm1.salaryHikeCalculation(44000.0)); System.out.println(sm2.salaryHikeCalculation(55050.0)); } } ==> Instance method can always access/invoke through the object only. whereas the static method can always access through either object or through class. package AIT.IO.Java; class StaticMethod{ int eid; String ename; double salary; StaticMethod(int eid,String ename,double salary) { this.eid = eid; this.ename = ename; this.salary = salary; } // non-static method/instance method void display() { System.out.println("Id = "+eid); System.out.println("Name = "+ename); System.out.println("Salary = "+salary); } // static method static double salaryHikeCalculation(double salary) { return (salary * 1.5); } } public class MainClass { public static void main(String[] args) { StaticMethod sm1 = new StaticMethod(101,"Rajesh",23000.0); StaticMethod sm2 = new StaticMethod(201,"Reshma",21050.0); // System.out.println(sm1.salaryHikeCalculation(44000.0)); // System.out.println(sm2.salaryHikeCalculation(55050.0)); double res1 = StaticMethod.salaryHikeCalculation(12000.0); double res2 = StaticMethod.salaryHikeCalculation(13000); System.out.println(res1 + " "+res2); sm1.display(); sm2.display(); // StaticMethod.display(); } } =========================================== length() ==> instance method Syntax: str-obj.length() valueOf() ==> static method Syntax: String.valueOf(20) split() ==> Instance method "Java Programming Language" "Python is Object Oriented" Syntax: str-obj.split(separator) join() ==> static method Syntax: String.join(separator,arrayOfStrings) ===================================== Static Block: ============= ==> can be used for initialization of the static variables. class ClassA{ int x; // instance variable static int y; // static variable ClassA(int x) { this.x = x; this.y = 321; } void display() { SOP(x); SOP(y); } } public class MainClass{ public static void main(String[] args) { ClassA a1 = new ClassA(10); ClassA a2 = new ClassA(20); a1.display(); // x = 10 and y = 321 a2.display(); // x= 20 and y = 321 } } ==> When we have to initialize the static variable within the constructor, in this case for the static variable re-initialization of memory in heap can be triggered for object to object. To overcome this, we have to use "static block". ==> we can allowed to define multiple static blocks within the same class. When we can multiple static blocks in the class: the order of static block execution is same as order of static block definition. package AIT.IO.JFSD; class StaticBlock{ int x; static int y; // constructor can always allow to initialize the instance variables only. StaticBlock(int x) { this.x = x; } // to initialize the static variable, static block can be used. static { // x = 112; y = 123; System.out.println("Hello"); } void display() { System.out.println("x = "+x); System.out.println("y = "+y); } static { System.out.println("Hi"); } } public class MainClass { public static void main(String[] args) { StaticBlock sb = new StaticBlock(100); sb.display(); } } ====================================================== class MyClass{ static void m1(){ System.out.println("Hello); } } obj.m1(); MyClass.m1();