Methods in java
Example no.1 :
public class Test {
void m1(int a,char ch) {
[Link]("m1 method");
[Link](a);
[Link](ch);
}
static void m2(String str, double d) {
[Link]("m2 method");
[Link](str);
[Link](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) {
[Link]("m1 method");
}
static void m2(Y y, Student s) {
[Link]("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) {
[Link]("m1 method");
[Link](a);
[Link](ch);
}
static void m2(String str, double d) {
[Link]("m2 method");
[Link](str);
[Link](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() {
[Link]("m1 method");
}
void m1() {
[Link]("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
[Link]("m1 method");
m2();
}
void m2() {
m3(10);
[Link]("m2 method");
}
void m3(int a) {
[Link]("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) {
[Link](x + y); //local variables
[Link](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();
[Link](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>();
[Link](new Emp(111, "ratan"));
[Link](new Emp(444, "durga"));
[Link](new Emp(333, "shubham"));
[Link](new Emp(222, "shivraj"));
[Link](al);
for (Emp e : al) {
[Link]([Link] + " " + [Link]);
}
EMPLOYEE CLASS
public class Emp implements Comparable {
int eid;
String ename;
public Emp(int eid, String ename) {
super();
[Link] = eid;
[Link] = ename;
}
@Override
public String toString() {
return "Emp [eid=" + eid + ", ename=" + ename + "]";
}
@Override
public int compareTo(Object o) {
Emp e = (Emp) o;
if (eid == [Link])
return 0;
else if (eid > [Link])
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>();
[Link](new Emp(111, "ratan"));
[Link](new Emp(444, "durga"));
[Link](new Emp(333, "shubham"));
[Link](new Emp(222, "shivraj"));
[Link](al);
for (Emp e : al) {
[Link]([Link] + " " + [Link]);
}
*Employee Object
public class Emp implements Comparable<Emp> {
int eid;
String ename;
public Emp(int eid, String ename) {
super();
[Link] = eid;
[Link] = ename;
}
@Override
public String toString() {
return "Emp [eid=" + eid + ", ename=" + ename + "]";
}
@Override
public int compareTo(Emp e) {
return [Link]([Link]);
}
}