private final?

U

Ulf Nordlund

It is possible to declare a method as "private final", but would you
ever actually do that?
/ulf
 
S

Stefan Schulz

It is possible to declare a method as "private final", but would you
ever actually do that?

With methods this combination makes little sense, yes. I do, however,
rather frequently use private final fields. Forbidding it for methods
has no real gain, however.
 
X

xarax

Stefan Schulz said:
With methods this combination makes little sense, yes. I do, however,
rather frequently use private final fields. Forbidding it for methods
has no real gain, however.

--
In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
--- Rear Admiral Grace Murray Hopper

Rest In Peace, Grace.

Java is the COBOL of the new millenium.
 
M

Mike Schilling

Stefan Schulz said:
With methods this combination makes little sense, yes. I do, however,
rather frequently use private final fields. Forbidding it for methods
has no real gain, however.

One could argue that private methods are implictly final, just as interface
methods are implictly public and abstract. In both cases, there's no need
to put in the implicit qualifier but no real harm in it either.
 
T

Thomas G. Marshall

Ulf Nordlund coughed up:
It is possible to declare a method as "private final", but would you
ever actually do that?
/ulf


Might as well ignore the ponderrings and go straight to the JLS:


<verbatim except for bold marks>

8.4.3.3 final Methods

A method can be declared final to prevent subclasses from overriding or
hiding it. It is a compile-time error to attempt to override or hide a final
method.

A *private method* and all methods declared in a final class (§8.1.1.2) are
*implicitly final*, because it is impossible to override them. It is
permitted but not required for the declarations of such methods to
redundantly include the final keyword.

It is a compile-time error for a final method to be declared abstract.

</verbatim>
 
J

Jon Caldwell

Mike said:
One could argue that private methods are implictly final, just as interface
methods are implictly public and abstract. In both cases, there's no need
to put in the implicit qualifier but no real harm in it either.
I use this frequently for Log4J Logging categories.
 
J

Joona I Palaste

I'm sorry, I don't know what "this" refers to.

It's an English demonstrative pronoun, first person, singular. It means
"the thing I am talking about". In Java, it's a keyword that acts as a
reference to the current object.
Whoops, sorry. That wasn't what you meant, was it? =)
 
T

Tony Morris

Mike Schilling said:
One could argue that private methods are implictly final, just as interface
methods are implictly public and abstract. In both cases, there's no need
to put in the implicit qualifier but no real harm in it either.

A reflective lookup of an interface method reveals that it is indeed
abstract and public - the same cannot be said for a private method being
final.
A more accurate statement might be, "A private method cannot be overridden".

Here's some fun:
What's the output?
Change the access modifier on printS() - what's the output now?

class T
{
private final String s;


private void printS()
{
System.out.println(s);
}


T(String s)
{
this.s = s;
}


public static void main(String[] args)
{
new T("main").m();
}

private void m()
{
new T("m")
{
void method()
{
printS();
}
}.method();
}
}
 
T

Thomas G. Marshall

Joona I Palaste coughed up:
It's an English demonstrative pronoun, first person, singular. It
means "the thing I am talking about". In Java, it's a keyword that
acts as a reference to the current object.
Whoops, sorry. That wasn't what you meant, was it? =)

I don't know what "that" refers to.
 
M

Mike Schilling

Tony Morris said:
A reflective lookup of an interface method reveals that it is indeed
abstract and public - the same cannot be said for a private method being
final.

You're right, and this is somewhat odd. For methods,

"private"

and

"private final"

mean the same thing, since private methods can never be overridden, but the
two can be distinguished using reflection.
 
V

Virgil Green

Joona said:
It's an English demonstrative pronoun, first person, singular. It
means "the thing I am talking about". In Java, it's a keyword that
acts as a reference to the current object.
Whoops, sorry. That wasn't what you meant, was it? =)

He asked what "this" refers to, not what "this" is.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top