Methods in java
Example no.1 :
public 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 static void main(String[] args) {
Test t = new Test();
t.m1(999,'S');
Test.m2("Shubham", 102.33);
Method with Expecting ObjectExample no. 2
public class X{} separate class
public class Emp{} separate class
public class Y{} separate class
public class Student{} separate class
public class Test {
void m1(X x, Emp e) {
System.out.println("m1 method");
}
static void m2(Y y, Student s) {
System.out.println("m2 method");
}
public static void main(String[] args) {
Test t = new Test();
X x = new X();
Emp e = new Emp();
t.m1(x, e);
Y y = new Y();
Student s = new Student();
Test.m2(y, s) }
}
Example 3
public 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 static void main(String[] args) {
Test t = new Test();
t.m1(111, 's');
Test.m2("shubham", 10.5);
/*in class level we passing the double value, int
value
* but in project level the method do not expecting
any double, float any value
* In project level expecting the Objects e.g. void
m1(int a) this is not expecting int value
* this is expecting void m2(emp e) object
*
*/
}
Same Methods with Same Singnature Not
allowed
Example 4
package methodsByRatan;
public class Test {
void m1() {
System.out.println("m1 method");
}
void m1() {
System.out.println("m2 method");
}
public static void main(String[] args) {
}
package methodsByRatan;
public class Test {
void m1() {
m2(); // this is method calling by another using by
method
System.out.println("m1 method");
m2();
}
void m2() {
m3(10);
System.out.println("m2 method");
}
void m3(int a) {
System.out.println("m3 method");
}
public static void main(String[] args) {
Test t = new Test();
t.m1();
}
/*
* two method with same signature is not allowed returen
type is mandatory
* declaring the method in the method is called as Inner
method & java not
* support inner class method
*
*
*/
}
package methodsByRatan;
public class Test {
//instance variable
int x = 100;
int y = 200;
void add(int x, int y) {
System.out.println(x + y); //local variables
System.out.println(this.x + this.y); //to represent
the instance var use this keywrd
//inside the static method this keyward not allowed
}
public static void main(String[] args) {
Test t = new Test();
t.add(1000, 2000);
}
/*
* two method with same signature is not allowed returen
type is mandatory
* declaring the method in the method is called as Inner
method & java not
* support inner class method
*
*
*/
}
COMPARABLE() METHODS : SORTING EID WITH THE HELP OF
NORMAL VERSION
public class Coll_Sort_EmpImplementsComparableDemo {
//sorting the eid with normal version
public static void main(String[] args) {
ArrayList<Emp> al = new ArrayList<Emp>();
al.add(new Emp(111, "ratan"));
al.add(new Emp(444, "durga"));
al.add(new Emp(333, "shubham"));
al.add(new Emp(222, "shivraj"));
Collections.sort(al);
for (Emp e : al) {
System.out.println(e.eid + " " + e.ename);
}
EMPLOYEE CLASS
public class Emp implements Comparable {
int eid;
String ename;
public Emp(int eid, String ename) {
super();
this.eid = eid;
this.ename = ename;
}
@Override
public String toString() {
return "Emp [eid=" + eid + ", ename=" + ename + "]";
}
@Override
public int compareTo(Object o) {
Emp e = (Emp) o;
if (eid == e.eid)
return 0;
else if (eid > e.eid)
return 1;
else
return -1;
}
2) Comparable() method : sorting ename with help of generic version
public class Coll_Sort_EmpImplementsComparableDemo {
// sorting the ename with generic version
public static void main(String[] args) {
ArrayList<Emp> al = new ArrayList<Emp>();
al.add(new Emp(111, "ratan"));
al.add(new Emp(444, "durga"));
al.add(new Emp(333, "shubham"));
al.add(new Emp(222, "shivraj"));
Collections.sort(al);
for (Emp e : al) {
System.out.println(e.eid + " " + e.ename);
}
*Employee Object
public class Emp implements Comparable<Emp> {
int eid;
String ename;
public Emp(int eid, String ename) {
super();
this.eid = eid;
this.ename = ename;
}
@Override
public String toString() {
return "Emp [eid=" + eid + ", ename=" + ename + "]";
}
@Override
public int compareTo(Emp e) {
return ename.compareTo(e.ename);
}
}