| | |
call a method in an object, by giving the same object as the parameter
![]() |
•
•
Join Date: Sep 2006
Posts: 68
Reputation:
Solved Threads: 0
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..
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..
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.
is just sufficient.
It's not that doing so will not work but it's logically just wrong.
Java Syntax (Toggle Plain Text)
public int saveObject(Operator _operator){ // Code }
•
•
•
•
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...???
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.
![]() |
Similar Threads
- how to call a method defined in another file? (Java)
- Error: uncaught exception: Permission denied to call method XMLHttpRequest.open (HTML and CSS)
- Generic method parser / invoker (C#)
- Beginner working with tab-delimited text file (Perl)
- convert from 'ref double[]' to 'ref object' (C#)
- Calling Object Methods within a LinkedList (Java)
Other Threads in the Java Forum
- Previous Thread: recursion problem...
- Next Thread: date Validation
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows





