There's this, and the fact that it's effectively unenforceable. String t = ((Supplier<String>) () -> { final String s = myMethod(); return s; }).get();
So you keep the compiler happy but the desire to force only final retention of the reference (which, like Raffaello, I don't understand the use-case for) can be trivially circumvented. -- Aaron Scott-Boddendijk On Thu, Feb 3, 2022 at 9:09 AM Raffaello Giulietti < raffaello.giulie...@gmail.com> wrote: > Hello, > > I don't get why the author of myMethod() would/should be interested in > forcing the caller of the method to declare the variable b to be final. > Stated otherwise, what is the problem addressed by this suggestion? > Have you some specific case in mind? > > > Greetings > Raffaello > > > On 2022-02-02 20:27, Alberto Otero RodrÃguez 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. >