Methods: Inside the class, directly writing the logics are not allowed. We need to declare methods to write the logics of the application.
The return statement is a control flow statement that terminates the execution of method and return control to its caller. When return type if any method is void, then the method does not return anything.
Example I:
public class MethodDemo1 {
int a = 10;
int b = 20;
//System.out.println(a+b); // not valid
void add() {
System.out.println(a+b);
}
public static void main(String[] args) {
MethodDemo1 t = new MethodDemo1();
t.add();
}
}
There are two types of methods.
- Instance method: How to call the instance variable and method: memory allocated during object creation. If it is same area ( instance method to instance area), access the instance method directly and if it is different area ( instance method to static area) access the instance method by using object (instance methods are linked with objects)
- Static method: How to call static area?: memory allocated during class loading: Do not bother about area and access the static method by using class name. (static methods are linked with .class files)
Every method contains three parts:
- Void m1(); called method declaration
- Logic; called the method implementation
- t.m1(); called method calling
Example II: (without parameters)
package com.cerotid.method;
public class MethodDemo2 {
void m1() { // method declaration
System.out.println("m1 method"); // method implementation
}
static void m2() {
System.out.println("m2 method");
}
public static void main(String[] args) {
MethodDemo2 t = new MethodDemo2();
t.m1(); // method calling
MethodDemo2.m2();
}
}
Example III:(with parameters) method is expecting primitives
package com.cerotid.method;
class Test{
void m1(int a, char ch) {
System.out.println("m1 method");
System.out.println(a);
System.out.println(ch);
}
static void m2(String str, double d) {
System.out.println("m2 method");
System.out.println(str);
System.out.println(d);
}
}
public class MethodDemo3 {
public static void main(String[] args) {
Test t = new Test();
t.m1(10, 'a'); //call the instance method by using reference variable (different area)
Test.m2("Ram", 10.5); //call static method by using class name
} //if the method is expecting argument, pass that value whenever you are calling method
}
Output:
m1 method
10
a
m2 method
Ram
10.5
Example IV: Method is expecting object (parameters as objects)>> create the object and pass that object
package com.cerotid.method;
class X{}
class Y{}
class Student{}
class Emp{}
class Test1{
void m1(X x, Emp e) { // method is expecting object
System.out.println("m1 method");
}
static void m2(Y y, Student s) {
System.out.println("m2 method");
}
}
public class MethodDemo4 {
public static void main(String[] args) {
Test1 t = new Test1();
X x = new X();
Emp e1 = new Emp();
t.m1(x, e1);
t.m1(new X(), new Emp());
Y y = new Y();
Student s = new Student();
Test1.m2(y, s);
}
}
Example V: Methods with same signature
Example VI: Method calling m1 is calling m2 and m2 is calling m3 method
package com.cerotid.method;
class Test5{
void m1() {
m2();// // instance method calling m3 m2
System.out.println("m1 method");
m2(); //m3 m2
}
void m2() {
m3(10);
System.out.println("m2 method");
}
void m3(int a) {
System.out.println("m3 method");
}
}
public class MethodDemo5 {
public static void main(String[] args) {
Test5 t = new Test5();
t.m1();
}
}
Example VII: Method is expecting integer but there are both instance and local variables
package com.cerotid.method;
class Test6{
int x = 100; //instance variables
int y = 200;
void add(int x, int y) { // local variables, priority goes to local
System.out.println(x+y);
System.out.println(this.x + this.y);// to give the priority to instance variable
}
}
public class MethodDemo6 {
public static void main(String[] args) {
Test6 t = new Test6();
t.add(1000, 2000);
}
}
Output:
3000
300
Example VIII:
package com.cerotid.method;
class Test7{
int x = 100; //instance variables
int y = 200;
static void add(int x, int y) { // local variables
System.out.println(x+y);
// System.out.println(this.x + this.y); this is not valid
}
}
public class MethodDemo7 {
public static void main(String[] args) {
Test6 t = new Test6();
t.add(1000, 2000);
}
}
Example IX:java does support operator overloading
package com.cerotid.method;
class Test8{
}
public class MethodDemo8 {
public static void main(String[] args) {
System.out.println("ratan"+"durga");
System.out.println(10+20);
System.out.println(10+20+"durga"+"ratan"+100+200);
int a = 10;
int b = 20;
int c = 30;
System.out.println(a);
System.out.println(a+"...");
System.out.println(a+"..."+b);
System.out.println(a+"..."+b+"...");
System.out.println(a+"..."+b+"...."+c);
}
}
Output:
ratandurga
30
30durgaratan100200
10
10...
10...20
10...20...
10...20....30
Method vs return type
Example I: method vs returning primitives
package com.cerotid.method;
//method vs returning primitives
class Test9{
int m1() {
System.out.println("m1 method");
return 10;
}
float m2() {
System.out.println("m2 method");
return 10.5f;
}
static char m3() {
System.out.println("m3 method");
return 'a';
}
}
public class MethodDemo9 {
public static void main(String[] args) {
Test9 t = new Test9();
int x = t.m1();
System.out.println("return value of m(): "+x);
float f = t.m2();
System.out.println("return value of m2(): "+f);
char ch = Test9.m3();
System.out.println("return value of m3(): "+ch);
}
}
Output:
m1 method
return value of m(): 10
m2 method
return value of m2(): 10.5
m3 method
return value of m3(): a
Example II: Method vs returning object (very very important in project level)>>> create the object and return that object
package com.cerotid.method;
class Employee{}
class T{}
class Test10{
Employee m1() {
System.out.println("m1 method");
Employee e = new Employee();
return e;
}
T m2() {
System.out.println("m2 method");
T t1 = new T();
return t1;
}
static String m3() {
System.out.println("m3 method");
return "ratan";
}
}
public class MethodDemo10 {
public static void main(String[] args) {
Test10 t = new Test10();
Employee e1 = t.m1();
System.out.println(e1);
T t1 = t.m2();
System.out.println(t1);
String str = Test10.m3();
System.out.println(str);
}
}
Output:
m1 method
com.cerotid.method.Employee@15db9742
m2 method
com.cerotid.method.T@6d06d69c
m3 method
ratan
Example III: Method vs returning current class (i.e. same class) (returning same class object) very very important
class Test11{
Test11 m1() {
System.out.println("m1 method");
Test11 t = new Test11(); // 1st approach
return t;
//return new Test11();
}
Test11 m2(){
System.out.println("m2 method");
return this; // 2nd approach, recommended 2nd approach
}
}
public class MethodDemo11 {
public static void main(String[] args) {
Test11 t1 = new Test11();
Test11 t2 = t1.m1();
Test11 t3 = t1.m2();
}
}
Output:
m1 method
m2 method
Example IV: Method vs returning variable (very very important)
Case 1: When the application contain instance variable and local variable
class Test12{
int a = 100; //instance variable
int m1(int a) { //local variable
return a; // priority goes to local variable
}
}
public class MethodDemo12 {
public static void main(String[] args) {
Test12 t = new Test12();
int a1 = t.m1(10);
System.out.println(a1);
}
}
Output: 10
case 2: when the application contain only the instance variable not the local variable then priority goes to instance variable
class Test12{
int a = 100; //instance variable
int m1() { //no local variable
return a;
}
}
public class MethodDemo12 {
public static void main(String[] args) {
Test12 t = new Test12();
int b1 = t.m1();
System.out.println(b1);
}
}
Output: 100
Case 3: when the application contain instance variable and local variable, but you want to give priority to local variable use this keyword
class Test12{
int a = 100;
int m1(int a) {
return this.a; // to give the priority to local variable
}
}
public class MethodDemo12 {
public static void main(String[] args) {
Test12 t = new Test12();
int a1 = t.m1(100);
System.out.println(a1);
}
}
Output: 100
public class MethodDemo12 {
Method representation:
package com.cerotid.fundamentals;
public class ReturnStatement {
public static void main(String[] args) {
printMessage();
add(10, 20);
}
public static void printMessage() {
System.out.println("This is the first method");
}
public static void add(int a, int b) {
System.out.println(a+b);
}
}
Output:
This is the first method
30
package com.cerotid.fundamentals;
public class ReturnStatement {
public static void main(String[] args) {
printMessage();
int sum = add(10, 20);
System.out.println(sum);
}
public static void printMessage() {
System.out.println("This is the first method");
}
public static int add(int a, int b) {
return a + b;
}
}
package com.cerotid.fundamentals;
public class ReturnStatement {
public static void main(String[] args) {
String shouting = caps("Why are you reading my diary?");
System.out.println(shouting);
}
public static String caps(String s) {
return s.toUpperCase();
}
}
Rules:
- Duplicate methods are not allowed
- Two methods with same signature (method name with parameter) is not allowed
- Return type is mandatory
- Java does not support inner methods (declaring one method inside another method) but support inner class
- Method calling: I am m1();, I am calling m2();. When I call m2(), all the statement of m2 executed then only control returns to once again m1 (see example VI)
- If the application contain both instance and local variables with the same name, then the first priority goes to local, to represent the instance variable use 'this' keyword (see example VII)
- Inside the static method, 'this' keyword is not allowed
- Java does not support operator overloading but only one overloaded operator in java is + operator
- void m1(): in java method return type is mandatory and void represent return nothing. Return type may be primitive, class type, array type, enum type etc.
- If the method return type is other than void, we must return the value by using return statement and return statement must the last statement of the method
- Method is able to return value but holding that value is optional, however, it is recommended to hold that value and check the status of method
- If the method return type is object, create the object and return that value
- When the application contain both instance variable and local variable, priority goes to local variable
- Some notes:
- new: to create object
- this: to represent the current class object
- super: to represent parent class object
- instanceOf: to represent the type of object
- method: void, return
- source file: class, extends, interface, implements, package, import
Parameter passing and Returning a values from method:
package com.cerotid.methodconcept;
public class MyClass {
public static void main(String[] args) {
sayHello();
}
private static void sayHello() {
System.out.println("Hello Youtube");
}
}
Output:
Hello Youtube
package com.cerotid.methodconcept;
public class MyClass {
public static void main(String[] args) {
sayHello("Ram");
sayHello("Shyam");
sayHello("Hari");
}
private static void sayHello(String name) {
System.out.println("Hello: " + name);
}
}
Output:
Hello: Ram
Hello: Shyam
Hello: Hari
package com.cerotid.methodconcept;
public class MyClass {
public static void main(String[] args) {
add(10, 20);
add(230, 345);
add(300, 500);
}
public static void add(int a, int b) {
System.out.println(a + b);
}
}
Output:
30
575
800
package com.cerotid.methodconcept;
public class MyClass {
public static void main(String[] args) {
int sum1 = add(10, 20);
int sum2 = add(230, 345);
int sum3 = add(300, 500);
System.out.println(sum1);
System.out.println(sum2);
System.out.println(sum3);
}
public static int add(int a, int b) {
return a + b;
}
}
Output:
30
575
800
Method overloading example;
package com.cerotid.methodconcept;
public class MyClass2 {
public static void main(String[] args) {
System.out.println(add(2, 4));
System.out.println(add(20.56, 40.876));
System.out.println(add("Hello", "Ram"));
}
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
public static String add(String a, String b) {
return a + b;
}
}
Output:
6
61.43599999999999
HelloRam
toString():
toString():
package com.cerotid.encapsulation;
public class Employee {
int eid;
String ename;
double esalary;
public Employee(int eid, String ename, double esalary) {
super();
this.eid = eid;
this.ename = ename;
this.esalary = esalary;
}
public static void main(String[] args) {
Employee e1 = new Employee(111, "Ram", 100000.56);
Employee e2 = new Employee(222, "Shyam", 100000.24);
System.out.println(e1);
System.out.println(e2);
System.out.println(e1);//compiler writes here e1.toString()
System.out.println(e2);//compiler writes here e2.toString()
}
}
Output:
com.cerotid.encapsulation.Employee@15db9742
com.cerotid.encapsulation.Employee@6d06d69c
As you can see in the above example, printing e1 and e2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. i.e. we need to call toString method explicitly in order to see return the specified values. See the example below.
package com.cerotid.encapsulation;
public class Employee {
int eid;
String ename;
double esalary;
public Employee(int eid, String ename, double esalary) {
super();
this.eid = eid;
this.ename = name;
this.esalary = esalary;
}
@Override
public String toString() {
return "Employee [eid=" + eid + ", ename=" + ename + ", esalary=" + esalary + "]";
}
public static void main(String[] args) {
Employee e1 = new Employee(111, "Ram", 100000.56);
Employee e2 = new Employee(222, "Shyam", 100000.24);
System.out.println(e1);
System.out.println(e2);
}
}
Output:
Employee [eid=111, ename=Ram, esalary=100000.56]
Employee [eid=222, ename=Shyam, esalary=100000.24]
No comments:
Post a Comment