• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pass the current object as a parameter to another method?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ..
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
He ought to have given the setRegister object Pupil as a parameter type, rather than School.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic