call a method in an object, by giving the same object as the parameter

Reply

Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

call a method in an object, by giving the same object as the parameter

 
0
  #1
Jan 26th, 2007
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..
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: aniseed is an unknown quantity at this point 
Solved Threads: 6
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: call a method in an object, by giving the same object as the parameter

 
0
  #2
Jan 26th, 2007
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.
  1. public int saveObject(Operator _operator){
  2. // Code
  3. }
is just sufficient.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 68
Reputation: rpjanaka is an unknown quantity at this point 
Solved Threads: 0
rpjanaka rpjanaka is offline Offline
Junior Poster in Training

Re: call a method in an object, by giving the same object as the parameter

 
-1
  #3
Jan 26th, 2007
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...???
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 353
Reputation: aniseed is an unknown quantity at this point 
Solved Threads: 6
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: call a method in an object, by giving the same object as the parameter

 
0
  #4
Jan 26th, 2007
Originally Posted by rpjanaka View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC