'final' for get/set methods?

A

andreyvul

Should set methods for private final fields be declared final or not?
I.e. is this correct:
public class foo {
private final bar baz;
public foo(bar q) {
baz = q;
}
public final bar getBaz() {
return baz;
}
}

or should it be
public class foo {
private final bar baz;
public foo(bar q) {
baz = q;
}
public bar getBaz() {
return baz;
}
}

?
Which is better, factoring in speed, maintainability, ease of coding/
debugging, size, etc.?
 
D

Daniel Pitts

andreyvul said:
Should set methods for private final fields be declared final or not?
I.e. is this correct:
public class foo {
private final bar baz;
public foo(bar q) {
baz = q;
}
public final bar getBaz() {
return baz;
}
}

or should it be
public class foo {
private final bar baz;
public foo(bar q) {
baz = q;
}
public bar getBaz() {
return baz;
}
}

?
Which is better, factoring in speed, maintainability, ease of coding/
debugging, size, etc.?
"final" on a method only means that you can't override that method. If
your class is designed for inheritance, that might mean something,
otherwise it doesn't matter.

Speed isn't affected.
Maintainability isn't affected if you use final as intended.
Ease of coding is only affected in that you can't override that method.
Not often a good idea in practice, unless the classes invariants rely on
that method behaving exactly right.
debugging and size aren't affected.
 
R

RedGrittyBrick

Peter said:
[...]
I'm sorry I offended you. That really wasn't the point - I am not
actually calling anyone, certainly not you, a wimp.

So what is the point of the statement "don't be a wimp"? Either you
feel that behaving contrary to your recommendation is being a wimp, or
you don't. If you don't, then what relevance is "don't be a wimp"? And
if you do, then how are you _not_ calling someone who disagrees with you
a wimp?

I accept that you didn't intend to call anyone names, but the fact is
that either you made a completely irrelevant statement or you did in
fact engage in name-calling. I don't see any other way to interpret the
statement. Please feel free to elaborate if you do.

Please let it go Pete.
 
B

Ben Phillips

Peter said:
I realize our point of view on that is probably irreconcilable. [...]

It's practically impossible to get anyone to even bother to _try_ to
see your viewpoint when you assert your own viewpoint through
name-calling.

I'm happy to have a difference of opinion, but you've removed any
motivation for me to even try to understand your own perspective.
You've taken what could have been a philosophical discussion and turned
it into a vehicle for personal insults.

Chutzpah! With one thousand extra bonus points for irony.
 
R

Roedy Green

Should set methods for private final fields be declared final or not?

I use IntelliJ Idea whose lint puts finals on any non-overridden
get/set.

I thought this was goofy at first, now I like it.

The advantages:

1. when I see the final, I know there are no overrides to worry about
should I modify the method.

2. the code is faster and smaller.

3. when I make up a new method I get an error if the name is already
in use. I must explicitly remove the final in order to override.

This works fine for my own code where it is trivially easy to remove
finals later. However it is pain in the behind for third parties
using my code. If they remove the final, the next version of the base
classes they install will put it back again.
 
T

Tim B

andreyvul said:
Should set methods for private final fields be declared final or not?

you cannot assign a value to a final field through a set method. The code
will not compile.
I.e. is this correct:
public class foo {
private final bar baz;
public foo(bar q) {
baz = q;
}
public final bar getBaz() {
return baz;
}
}

or should it be
public class foo {
private final bar baz;
public foo(bar q) {
baz = q;
}
public bar getBaz() {
return baz;
}
}

neither of these examples contains a setter for your final field baz. Did
you mean a get method?
 
R

Roedy Green

you cannot assign a value to a final field through a set method. The code
will not compile.

I think he meant to ask
Should set methods for private fields be declared final or not?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top