Java Methods: ============= Is java support functions? ========================== No everything can define within the class. Is java support methods? ======================== Yes Method Vs Function: =================== function ==> is a named block accept data perform the functionality and return a value. ==> the function can always allow to define in outside of the class. method ==> is a named block can accept data perform the functionality and return a value. method can always allow to define within the class. Definition of the method: ========================= ==> method is the member of the class. ==> the method can be invoked through the object. Syntax: object-name.method-name(); //method calling or method invoking ==> to define the method, Syntax: return-type method-name(data which want to accept represent with type and name with comma separation) { // scope of the method return value/name of the value(variable); } ==> Methods can be classified into two types: 1) Pre-defined methods/Built-in methods ex: println(), print(), printf(), subString() etc. 2) User-defined methods ==> according to the user/programmer requirement, the method which are creating are called "user-defined" methods. Note: ===== Methods can define above or below to the main(). Methods can be invoked/called within the main() package AshokIT.OOPs; public class MethodDefinitions { // creating the method without data and no returning public static void method1() { int a = 100; int b = 200; // local variables System.out.println("The Sum = "+(a+b)); method2("Karthik"); } // method with data/parameter/argument static void method2(String name) { System.out.println("Hello,"+name+" Good Afternoon"); } // method with parameter and return type static int method3(int a, int b) { int s = a+b; return s; } public static void main(String[] args) { // method calling method1(); method2("Ravi"); System.out.println("The Return Value of the method = "+method3(230,121)); int result = method3(999,777); System.out.println("The Return value of the method = "+result); } } ================================= Nesting of methods: =================== ==> Java can support the writing of a method within another method. ==================================================== ==> If the class with some members are allowed to define/initialize in outside the class through the object. ==> Sometimes, the data members can initialize with inappropriate data or there is chance that other people might modify your data expectations. In this case, we should restrict class members to access in outside the class using "private" keyword. Syntax: private datatype member-name; ==> to define/initialize these private members, we need to use "getter and setter methods". ==> by default getter and setter methods are public type. package AshokIT.OOPs; class Student{ private int sid; private String sname; private char sgrade; void display(String sname) { System.out.println("The Data of "+sname+" is = "); System.out.println("Student Id = "+sid); System.out.println("Student name = "+sname); System.out.println("Student Grade = "+sgrade); } } public class StudentClass { public static void main(String[] args) { Student s1 = new Student(); // s1.sid = 10; // s1.sname = "Keerthi"; // s1.sgrade = 'A'; // s1.display(s1.sname); Student s2 = new Student(); // s2.sid = -12; // s2.sname = null; // s2.sgrade = 'A'; // // s2.display(s2.sname); } }