Constant methods in Java

Mariell Hoversholm mariell.hoversholm at paf.com
Mon Feb 7 06:52:19 UTC 2022


Hi Alberto,

This feels like a rather rare requirement. I prefer making my locals final,
but don't see any reason why they would _need_ to be.
Could you elaborate on an example use case for this?

On Wed, 2 Feb 2022 at 20:33, Alberto Otero Rodríguez <albest512 at hotmail.com>
wrote:

> I have a suggestion. I think it would be interesting creating constant
> methods in Java.
>
> I mean methods declared like this:
>
> public const String myMethod() {
>   String a = "a";
>   a = a + "b";
>   return a;
> }
>
> So that the response of the method is forced to be assigned to a final
> variable.
>
> This would be ok:
> final String b = myMethod();
>
> But this would throw a compilation error:
> String c = myMethod();
>
> What do you think? It's just an idea.
>


More information about the amber-dev mailing list