Object As Parameter


In java we can pass any parameter be it standard data types or an object or class.

In this example we will demonstrate you how to pass an object as parameter in a method.


eg



1.    myClass.java

package object_parameters;

public class myClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		test t1=new test(20);
		test t2=new test(30);
		test t3=new test(20);
		
		System.out.println("Checking 1 and 2 = "+t1.checkVariable(t2));
		System.out.println("Checking 1 and 3 = "+t3.checkVariable(t3));
		System.out.println("Checking 2 and 3 = "+t2.checkVariable(t3));
	}

}

2. test.java
package object_parameters;

public class test {

	public int i;
	
	public test(int k) {
		this.i=k;
	}
	
	
	public boolean checkVariable(test t) {
		boolean check=false;
		
		if((t.i)==this.i) {
			check=true;
		}else check=false;
		
		return check;
	}
}

output:
Checking 1 and 2 = false
Checking 1 and 3 = true
Checking 2 and 3 = false



Related Tutorials