Sten Kin
Sten Kin

Reputation: 2615

Java get set one liner

I'd like to do a get and set if null like so.

Foo foo = new Foo();
...
String test = foo.getTest() == null ? foo.setTest("this") : foo.getTest();

The thing is set is a void method. What the right way to set this String test to ?

Upvotes: 0

Views: 555

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280102

Don't use a one liner.

String test = foo.getTest();
if (test == null) {
    test = "this";
    foo.setTest(test);
}

Upvotes: 3

Related Questions