DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   call a method in an object, by giving the same object as the parameter (http://www.daniweb.com/forums/thread68328.html)

rpjanaka Jan 26th, 2007 4:58 am
call a method in an object, by giving the same object as the parameter
 
hi all,

i want to know about simple java concept,


public class Operator{


//this is a bean class and there is a method to insert object of this class, into the database.

public int saveObject(Operator _operator){

//here are codes for insert the given object in the database.
}//end of the method


}//end of the class






then this is the testing class include main method.



public class testOperator{

public static void main(String[] args){

//create an operator object
Operator op = new Operator();


//then insert this object by calling the "saveObject" method in the //same object

op.saveObject(op);

}
}



Above it save the object using the method in that object,

is this possible.......????

But when I test this, it was properly worked......


pleas anyone can explain me the concept behind this...


thanks..

aniseed Jan 26th, 2007 5:32 am
Re: call a method in an object, by giving the same object as the parameter
 
Your method signature is, kind of, redundant. When you are calling a method on an object, you don't need to pass the same object to it. Whatever information you need about the object is already available within the method. There is a keyword called "this" to refer to the current object within the method.

It's not that doing so will not work but it's logically just wrong.
public int saveObject(Operator _operator){
    // Code
}
is just sufficient.

rpjanaka Jan 26th, 2007 8:04 am
Re: call a method in an object, by giving the same object as the parameter
 
I want to know, is this possible, because in that case, that operator object do some operation for itself,

is this possible...?

and is this a bad programming practice...???

aniseed Jan 26th, 2007 12:35 pm
Re: call a method in an object, by giving the same object as the parameter
 
Quote:

Originally Posted by rpjanaka (Post 306642)
I want to know, is this possible, because in that case, that operator object do some operation for itself,

is this possible...?

and is this a bad programming practice...???

It is possible and it is bad/redundant code design.

If an object has to do some operation on itself, it can make use of the this keyword. There is no need to pass a self-reference. That's simply a bad idea.


All times are GMT -4. The time now is 10:00 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC