0

I will like to know if there is a way to do the second or third return statement in Java

Example using C#:

I have this class with properties

class Properties
{
    public int ResponseCode { get; set; }

    public string ResponseMessage { get; set; }

    public string Name { get; set; }

    public string Academics { get; set; }
}

I did it like this at the beginning:

    public static Properties CreateStudentFirstWay()
    {
        Properties properties = new Properties();
        properties.Name = "Michael";
        properties.Academics = "Information";
        properties.ResponseCode = 0000;
        properties.ResponseMessage = "Created";
        return properties;
    }

I use this from time to time

    public static Properties CreateStudentSecondWay()
    {
        Properties properties = new Properties()
        {
            Name = "Michael",
            Academics = "Information",
            ResponseCode = 0000,
            ResponseMessage = "Created"
        };

        return properties;
    }

And recently I have been doing it like this

    public static Properties CreateStudentThirdWay()
    {
        return new Properties()
        {
            Name = "Michael",
            Academics = "Information",
            ResponseCode = 0000,
            ResponseMessage = "Created"
        };
    }

We are able to do the last one in C# without a constructor, without a builder or even without the need to populate all the properties in the class.

Is there a way to accomplished this in Java without the need of a constructor or the builder?

Thanks!

D. Lawrence
  • 943
  • 1
  • 10
  • 23
Nusmo
  • 1
  • Builder Design Pattern .. to go – Soner from The Ottoman Empire May 02 '20 at 04:03
  • @madreflection _Never_ use "double brace". The "side effects" are catastrophic in all sorts of cases. – chrylis -cautiouslyoptimistic- May 02 '20 at 04:05
  • @chrylis-onstrike-: Point taken. I'm not a Java developer; it just looked like a possible duplicate, as the syntax looked very similar. I retracted the close vote based on your assertion. Probably not the place for this question, but how something so problematic even get into the language? – madreflection May 02 '20 at 04:09
  • @madreflection No worries, and I'd have re-opened. ;-) It's not that there's something "problematic in the language", it's simply abuse of a language feature (anonymous subclasses, which are used for strategy handlers and the like) to instead subclass a value type and apply no changes except adding initializer code--which nevertheless creates _a different class_ with all the attendant problems. Compare https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c – chrylis -cautiouslyoptimistic- May 02 '20 at 04:16

1 Answers1

0

Java does not support this syntax; you have to use a variable and call the various set methods. Groovy does have a syntax that provides this:

public static Properties createStudent() {
    return new Properties(
        name: "Michael",
        academics: "Information",
        responseCode: 0,
        responseMessage: "created"
    )
}

but if you want to do this with Java you'll need either a constructor with arguments or a builder. Lombok's @AllArgsConstructor can generate one for you.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152