The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
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.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 . . .
still getting an error . ....now whats wrong in this please explain ?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.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
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.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
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.naved momin wrote: . . .
lets forget the design error as if for now . . .
Campbell Ritchie wrote:He ought to have given the setRegister object Pupil as a parameter type, rather than School.
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.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
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 ?
. . . but it would be clearer if you called it "p" rather than "s" in the School class.John Jai wrote: . . . Good you changed the parameter of the register method from Student to Pupil . . . .
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?
Well done and thank you to those who could explain it better than me.naved momin wrote: . . .
thanks mathew and all others who gave there time for me ...thanks man ...
now i got that ... . . .
Andreas Zilinski wrote:
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.
Jeff Verdegan wrote:It's not clear what you mean here. We can't implement ObjectOutputStream, since it is a class, not an interface.
Jeff Verdegan wrote:Eh? Even less clear now.
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.
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 wrote:Right. I still don't understand what the problem is.
Okay, and what does this have to do with ObjectOutputStream?
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|