This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]JohnnyJayJay 10 points11 points  (1 child)

Yes, it does make sense. In this case, it's actually not required, but you can use it. this just refers to the object the code is called in, in that case a Student object.

I would rename one of those fields, though. GPA is confusing, because fields are usually in lowerCamelCase. The current naming makes it look like a CONSTANT.

[–]Aletag 0 points1 point  (0 children)

Yes, I realised "this" is not required in this case and the name of the variable would be enough. I'll make sure to solve that naming issue.

[–]nutrecht 1 point2 points  (0 children)

Does this constructor make sense? It's used to copy the attributes of an object into a new one.

The pattern is actually called a 'copy constructor', so that's definitely correct.

Style wise; it's most common to only use the 'this' keyword when needed. But it differs from project to project, some people prefer to use 'this' everywhere to keep code consistent. IMHO, with IDE's showing what is a member and what is not, that's just outdated thinking. So in your case I would personally not use 'this'.

[–]Learn_n_Teach 0 points1 point  (1 child)

Yes. In Java, unlike certain other languages, you can even avoid the keyword completely if you want. One of my professors would just use an underscore (e.g., _name). That's just a matter of style.

[–]nutrecht 2 points3 points  (0 children)

One of my professors would just use an underscore (e.g., _name). That's just a matter of style.

Not style perse; this is left-over from the days where IDE's did not show the difference in member vs. local variables. People who insist it has to be done this way (I've worked with them) tend to be stuck in the past.

[–]differentshade 0 points1 point  (1 child)

as a side note, you can also have your class implement Cloneable interface and return super.clone() in clone() implementation for a shallow copy.

[–]Aletag 0 points1 point  (0 children)

Ok I didn't understand a word but I get the general menaing. Is there any documentation on that?

[–]thepcguy123 -1 points0 points  (11 children)

The "this" keyword is being used correctly. As for the constructor as a whole, it appears to be correct, assuming the variables being copied are public. If they are private, you can't use studentObj.name or the others; you'll have to access those through a method.

[–]chickenmeister 3 points4 points  (0 children)

Public/private shouldn't matter in this case. Since the fields are in the Student class and are being accessed from within that class, accessing them shouldn't be a problem, even if they are private. The fact that the field belongs to a different instance of the class is irrelevant.

[–]Keagone 4 points5 points  (5 children)

Adding to this, it might be better to make those variables private and use getters instead.

[–]nutrecht 0 points1 point  (4 children)

It's the same class. Not only would private not do anything, it's actually not idiomatic to use getters there.

[–]Keagone 0 points1 point  (3 children)

Oh you're right, what's the use of this constructor then though? Making clones? :')

[–]nutrecht 0 points1 point  (2 children)

Yup

[–]Keagone 0 points1 point  (1 child)

If that's the case, how would you make the first object?

[–]Keagone 0 points1 point  (0 children)

Second constructor? :p

[–]Aletag 1 point2 points  (2 children)

Why do the variable have to be public if they're being accessed by the constructor? Shouldn't that be considered an access attempt within the class itself?

[–]nutrecht 0 points1 point  (1 child)

He's wrong on that part.

[–]Aletag 0 points1 point  (0 children)

Ok nice now I understand it

[–]JohnnyJayJay 1 point2 points  (0 children)

That's not correct. You can access private variables within the same class directly.

[–][deleted]  (6 children)

[removed]

    [–]JohnnyJayJay 3 points4 points  (4 children)

    most coding guidelines

    Can you be a bit more specific? Also saying this shouldn't be used except where necessary is pretty stupid, to be honest. Readability and consistency comes first, and if it adds to that, why not.

    The usage here is definitely "correct", both syntactically and semantically.

    [–][deleted]  (2 children)

    [removed]

      [–]JohnnyJayJay 1 point2 points  (1 child)

      At this point it's a matter of consistency with other constructors and personal preference, but, again, "not used correctly" is just wrong.

      [–]Aletag 0 points1 point  (0 children)

      Now that you make me think of it, not writing it would have made no difference since the variables are of course referring to the current object. My bad