aspose file tools*
The moose likes Java in General and the fly likes pass the current object as a parameter to another method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "pass the current object as a parameter to another method?" Watch "pass the current object as a parameter to another method?" New topic
Author

pass the current object as a parameter to another method?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

i m confuse with the 2nd use of this keyword
which is to pass the current object as a parameter to another method .
i have tried this ..


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
naved momin wrote: . . .
public static void main(String[] args) { . . .
mm.setValue(this); here the current object is mm ..right ? means i know its actually a reference to an actual object on the heap Main().

so by writing this what we are actually passing a reference variable or actual object . . .
No, not at all. There is at that point no "current object", and the compiler will not allow you to use the keyword this in that context.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
I have seen this sort of thingYou will see that the Pupil object referred to at the moment is being passed to a method of the School class, so the School can register this current Pupil.

What you might not notice here, is what an example of really bad design that is. You should not do work inside the Pupil class which the School class does, and using (this) inside a constructor can lead to dangerous errors in a multi-threaded environment.

You can serialise an object by using the writeObject method of an ObjectOutputStream. serialisingStream.writeObject(this); That takes the entire "current object" and tells the serialising Stream to write it. You will have set up the files, etc., in the Stream object's constructor.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:I have seen this sort of thingYou will see that the Pupil object referred to at the moment is being passed to a method of the School class, so the School can register this current Pupil.

What you might not notice here, is what an example of really bad design that is. You should not do work inside the Pupil class which the School class does, and using (this) inside a constructor can lead to dangerous errors in a multi-threaded environment.

You can serialise an object by using the writeObject method of an ObjectOutputStream. serialisingStream.writeObject(this); That takes the entire "current object" and tells the serialising Stream to write it. You will have set up the files, etc., in the Stream object's constructor.
still getting an error . ....now whats wrong in this please explain ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
I presume you realise you are repeating the design error I told you about earlier?

What does the error message say? That should tell you what mistake you are making.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:I presume you realise you are repeating the design error I told you about earlier?

What does the error message say? That should tell you what mistake you are making.

lets forget the design error as if for now , first i should be clear what " this " means when it is in parameter and ready to go to another method ?
error is
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi,

It seems that you are trying to add a Pupil object instead of a School object as expected by the setRegister() method. Can you check whether you desired to add a Pupil instead of a School in the setRegister() method?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
He ought to have given the setRegister object Pupil as a parameter type, rather than School.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
naved momin wrote: . . .
lets forget the design error as if for now . . .
Design errors are much more serious than syntax errors. A syntax error leaves you with a completely safe program, since it will never run. A design error will leave you with a dangerous program, ie one which can return the wrong result. People have died because of the wrong result being obtained from a computer program.

What the keyword this means in that line is the Pupil object you happen to be working inside, which you can also call the "current" instance of the class you are writing.

Please ask again if you don't understand.

Anybody else? Maybe somebody else can find words which you will find easier to understand.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Campbell Ritchie wrote:He ought to have given the setRegister object Pupil as a parameter type, rather than School.

That was I mentioned him to check. Sorry for my English!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
I think we are both trying to say the same things, but in a different way. It often helps to have two people explain things because different people understand different kinds of explanation.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:I think we are both trying to say the same things, but in a different way. It often helps to have two people explain things because different people understand different kinds of explanation.


Ok , i m now totally confuse , lets start it over again ..

what i want to do is that i m passing current object of pupil to a method called setRegister(Pupil s) so i have make a pupil reference in the parameter of the method .
so when i called this method using the school reference ...it turn out to be a error compile or syntax error .
why so , where i m wrong , do i didnt understand the " this " keyword or some other syntax error is there ?
my refresh code is
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Let me have a go then.

this doesn't mean "the last object I created", or anything like that. It means, roughly, "the object that the code is currently executing in".

Have a look at this partial code:

So the meaning depends on the context. Now, there's an important follow on to this. static methods don't operate on individual objects. As such, if you're in a static method, there is no such thing as this. this only has meaning within non-static methods.

Your main method is static (as it should be). At that point, therefore, this doesn't exist. If you want to pass the Pupil you've just created into the School you've just created, you just use the reference you've just declared:
There are some other problems related to static methods in the last bit of code you gave us. I assume the setRegister() method in School is intended to register a pupil with a particular school? In which case, it should not be static. As I said, static methods don't act on individual objects. A static method would only make sense there if you were adding Pupils to a collection that was shared between all Schools.

Finally, you've got a Pupil() method in Pupil that looks like it might be meant to be a constructor? If so, then it shouldn't be static, and it shouldn't have a return type. If it's not a constructor, what is it for?, as it doesn't seem to make much sense.

Does any of that help?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
hi,
Good you changed the parameter of the register method from Student to Pupil
naved momin wrote:it turn out to be a error compile or syntax error .
why so , where i m wrong , do i didnt understand the " this " keyword or some other syntax error is there ?

If you see the error it will give you like '"this" keyword cannot be used inside a static block'
Might be since static methods can be called using the class itself without creating an object, the "this" keyword that refers to an object cannot be used.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
John Jai wrote: . . . Good you changed the parameter of the register method from Student to Pupil . . . .
. . . but it would be clearer if you called it "p" rather than "s" in the School class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Static methods always operate on the class, not an object; if there are several instances of this class, how is a static method meant to know which to use? If there are no instances of this class, how is the static method meant to find an instance?
An instance can always find its class, so it can always find static members.

So you can go instance→ static, but you can't go static → instance. Since the keywords this and super are specifically intended to point to instances, they must not ne used in a static context.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Matthew Brown wrote:Let me have a go then.

this doesn't mean "the last object I created", or anything like that. It means, roughly, "the object that the code is currently executing in".

Have a look at this partial code:

So the meaning depends on the context. Now, there's an important follow on to this. static methods don't operate on individual objects. As such, if you're in a static method, there is no such thing as this. this only has meaning within non-static methods.

Your main method is static (as it should be). At that point, therefore, this doesn't exist. If you want to pass the Pupil you've just created into the School you've just created, you just use the reference you've just declared:
There are some other problems related to static methods in the last bit of code you gave us. I assume the setRegister() method in School is intended to register a pupil with a particular school? In which case, it should not be static. As I said, static methods don't act on individual objects. A static method would only make sense there if you were adding Pupils to a collection that was shared between all Schools.

Finally, you've got a Pupil() method in Pupil that looks like it might be meant to be a constructor? If so, then it shouldn't be static, and it shouldn't have a return type. If it's not a constructor, what is it for?, as it doesn't seem to make much sense.

Does any of that help?

thanks mathew and all others who gave there time for me ...thanks man ...
now i got that ...and finally i have run the code .
thanks again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
naved momin wrote: . . .
thanks mathew and all others who gave there time for me ...thanks man ...
now i got that ... . . .
Well done and thank you to those who could explain it better than me.
Andreas Zilinski
Greenhorn

Joined: May 07, 2012
Posts: 3
I have the same problem. I've read the replies and I undestand what does "this" stand for but I don't want to do a bad desing by using a reference to the current object in a constructor, as "Campbell Ritchie" said.

I've tried to implement ObjectOutputStream, but it asks me to send a OutputSteam object as a parameter for its constructor and when I wanted to create an instance I realized I had to override all its methods.

I would like to learn how to do this since I think I'll have to do it often (by the way, the app I'm working in at the moment will never run in multi-thread).

Thanks.

Greetings.

PS: sorry for the bad english.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Andreas, welcome to the Ranch!

Andreas Zilinski wrote:
I've tried to implement ObjectOutputStream,


It's not clear what you mean here. We can't implement ObjectOutputStream, since it is a class, not an interface.

but it asks me to send a OutputSteam object as a parameter for its constructor and when I wanted to create an instance I realized I had to override all its methods.


Eh? Even less clear now.

It would help if you could post your code, or just enough of it to show the problem--and SSCCE.

And, ideally, you'd start your own thread for your question. If you feel there's background in this thread that's relevant, post a link to it in your thread.
Andreas Zilinski
Greenhorn

Joined: May 07, 2012
Posts: 3
Jeff Verdegan wrote:It's not clear what you mean here. We can't implement ObjectOutputStream, since it is a class, not an interface.

You're right. I meant "use" the class, not implement it.

Jeff Verdegan wrote:Eh? Even less clear now.

ObjectOutputStream has 2 constructors. One of them requires no parameters (but it's protected, not public). The other one is public, but requires an OutputStream object as a parameter.

Here is my code (variables and methods are named in Spanish because as you may imagine, English is not my primary language)



Here, the constructor of Pasaje calls the method "agregarPasaje" from an instance of Viaje, which take an instance of Pasaje and adds it into an ArrayList.
IDE doesn't throw any error or exception.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Andreas Zilinski wrote:
Jeff Verdegan wrote:It's not clear what you mean here. We can't implement ObjectOutputStream, since it is a class, not an interface.

You're right. I meant "use" the class, not implement it.

Jeff Verdegan wrote:Eh? Even less clear now.

ObjectOutputStream has 2 constructors. One of them requires no parameters (but it's protected, not public). The other one is public, but requires an OutputStream object as a parameter.


Right. I still don't understand what the problem is.


Here is my code (variables and methods are named in Spanish because as you may imagine, English is not my primary language)



Here, the constructor of Pasaje calls the method "agregarPasaje" from an instance of Viaje, which take an instance of Pasaje and adds it into an ArrayList.
IDE doesn't throw any error or exception.


Okay, and what does this have to do with ObjectOutputStream?
Andreas Zilinski
Greenhorn

Joined: May 07, 2012
Posts: 3
Jeff Verdegan wrote:Right. I still don't understand what the problem is.

Okay, and what does this have to do with ObjectOutputStream?


Problem is, why does this lead to a bad design ? And how does ObjectOutputStream lead to a better design ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass the current object as a parameter to another method?