The Student Room Group

How to make a java method that makes a record

Or in other words initialises a record?
It depends on why you need to do this, and the structure of your code.

To answer straight up. Replace 'ClassType' with that of the class name.

public ClassType initialise() {
return new ClassType();
}

// Then you can have
var object = initialise(); // Will now equal a new ClassType object.

Note that var is only available from Java 10. It's also something that shouldn't be used unnecessarily. It basically infers the type for itself without it needing you to explicitly state it. Of course you can do:


ClassType object = initialise();


But that seems to render the use of a method to do this pointless.

Quick Reply

Latest