1

Just curious about little something that came to mind, but I haven't been able to find an answer to it by searching. Is it possible to return an instance with the return keyword and also set a variable within that instance in the same line?

For example, I tried doing:

public Person randomPersonHeight(Person person){
     Random r = new Random();
     return person.height = 5+(r.nextFloat()*1.5);//Imperial Measurements baby!
}

I wanted to set the persons height AND return the person in one swoop. But no surprise that didn't work. Of course it isn't important that I can do it in the single line. And, just wont stand for anything less!.. But, I found myself wanting to know there was any expression or different format that would make it possible.

DoctorDep
  • 1,611
  • 2
  • 11
  • 10
  • No. There is no way to write that expression on one line. Use two. – Michael Aug 05 '19 at 11:51
  • Well, if your `Person` has a constructor that takes a height, you could `return new Person(newHeight)`. But that's only if you have the appropriate constructor (and if you don't need anything else than the height). – Arthur Attout Aug 05 '19 at 12:14
  • it is possible: [15.25. Conditional Operator ? :](https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.25) - see my comment to [Henning's answer](https://stackoverflow.com/a/57358003/85421) if you really want an ugly way to do it (not very realistic/serious) – user85421 Aug 05 '19 at 12:26
  • Yep, I figured there wasn't a way to do this in a traditional way, because it didn't make logical sense to me but I'm glad I could ask everyone real quick if anyone else knew some niche magical expression I didn't know. Cheers! – DoctorDep Aug 05 '19 at 20:28

2 Answers2

0

You can't to that.

There's a common idiom where setter methods return the object itself, to make something like this possible. But you can't do it with just the language's native operators.


Except, of course, if you write

 person.height = 5+(r.nextFloat()*1.5); return person;

But if you ever let a code autoformatter near that, it probably won't survive ...

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73
  • 1
    "*you can't do it with just the language's native operators*" - do not underestimate developers imagination.... `return (person.height = 5+(r.nextFloat()*1.5)) > 0 ? person : null;` (and ability to make unreadable code [:-) ) – user85421 Aug 05 '19 at 12:17
  • [15.25. Conditional Operator ? :](https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.25) is a native operator, Optional is more like a class... and sure not a Person – user85421 Aug 05 '19 at 12:30
  • @CarlosHeuberger That code made me laugh by it's creativity. – DoctorDep Aug 05 '19 at 20:25
0

The other answers talked about setters but I think they misinterpreted your question to be about coding conventions (what you should do) while, if I understand you correctly, you asked about the Java language itself (what you can do).

Returning a value and setting it at the same time is no problem with the Java language.

Example

class Test
{
    int sum;

    public int sum(int a, int b)
    {
        return (sum=a+b);
    }

    public static void main(String[] args)
    {
    System.out.println(new Test().sum(1,2));
    }

} 
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • 2
    "Returning a value and setting it at the same time is no problem with the Java language" with the **heavy** caveat that the return type must equal the thing that you are setting. Which he is not. He is setting a double and hoping to return a person. – Michael Aug 05 '19 at 12:23
  • @Michael: You are right, I misinterpreted the question myself. – Konrad Höffner Aug 05 '19 at 12:25