• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"this" in constuctor problem

 
Ben Flowers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im really confused as to why this doesnt seem to work? My Class needs to have a constructor which copys another object passed as a parameter and a copy function which copys itself to another object. The constructor code works fine but the copy doesnt seem to pass on any data can anyone see why?


Cheers,
Ben
 
Stephan van Hulst
Bartender
Posts: 6320
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Target is a variable that holds a reference to a Person. When you assign a new Person to Target, you're overwriting the old reference.

When you call a method in Java, and pass a variable as an argument, you're not actually assigning the variable itself to the method parameter, but the value of the variable.

Also note that your coding style does not reflect widely accepted Java naming conventions. All your variables should start with a lower case letter.
 
Ben Flowers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Target is an Object though, Java passes an Object by refernce so it should effect the actual values right?
And i thought the java convention was to use lower case on primitive values and upper case on objects?
Cheers,
Ben
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Java passes everything by value. Confusion often arises because when you're talking about Objects it passes references by value.

The usual convention is to use capitals for classes, and lower case for variables and methods.
 
Ben Flowers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right thats annoying. I've done a work around by just using the setMethods() for the target value. Is there a more elegant way to do it?
Cheers,
Ben
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a copy constructor, you don’t need a copy() method. You would use the copy constructor like thisNo need for the Target field in the Person class at all.
Also, you don’t need to use get methods. In the present circumstance, the keywords this is redundant, so you can emit it.Note I have changed += 1 to ++. I have also got rid of the extraneous Capital Letters. I have not however got rid of another mistake, that of using tab for indenting. Get yourself a decent text editor which supports automatic indenting, and set it to convert tab to 4 spaces automatically.

There is an alternative design, that of making the Person class immutable. Then you don’t need copy constructors or the clone() method or anything.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you given those fields protected access? It would be better to give them private access and permit access via getXXX methods.
I would also change the char gender to an enum element.... because you can be sure there are only two genders (that is not quite true, but introducing hermaphrodite is probably surplus to requirements at this stage). Interestingly enough, you can change gender to an enum by changing a single word in your original Person class.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can enhance the Gender enum; the link I posted earlier should help you work out how to enhance it.
 
Ben Flowers
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its a part of a uni project, so i have to have the class the way it is. I.e. a cloning constructor, with a method which copies itself to another object and the protected variabled (which i totally agree is stupid) and the char gender.
Thanks for the suggestions guys,
Ben
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome. Try this for the copy methodNote the copy method comes to 1368 in the Campbell Ritchie classification of methods, and therefore ought to be static. Don’t implement an interfaces with a ... copy(Person) method in, otherwise it can’t be static.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you feel copy must be a static method of the class which takes an instance as a parameter. Why can't it be the case that any particular instance of Person is able to render a copy of itself? This seems more natural to me; it's the way I implement copy in my own classes.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Flowers wrote:Is there a more elegant way to do it?

Well, don't know about elegant, but:will definitely do the trick.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing is that a non-static method has to be called on an instance of the class. The method we were shown originally took a Person object as a parameter, and returned a copy which had nothing to do with being called on an object reference. It is rather like a factory method, which is usually static. That is why I thought it should be static. The way that method is written, there is no difference betweenandIn which case, why not static?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski’s solution is different; it is more like a clone() method. It has to be called on an instance, obviously.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic