File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark ""this" in constuctor problem" Watch ""this" in constuctor problem" New topic
Author

"this" in constuctor problem

Ben Flowers
Greenhorn

Joined: Sep 12, 2011
Posts: 17
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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

Joined: Sep 12, 2011
Posts: 17
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

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Sep 12, 2011
Posts: 17
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
You can enhance the Gender enum; the link I posted earlier should help you work out how to enhance it.
Ben Flowers
Greenhorn

Joined: Sep 12, 2011
Posts: 17
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Mar 17, 2011
Posts: 7892
    
  21

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: Oct 13, 2005
Posts: 39084
    
  23
Winston Gutkowski’s solution is different; it is more like a clone() method. It has to be called on an instance, obviously.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "this" in constuctor problem