1

If I have something like the code below as a constructor, is there a simple, shorthand way to do all the instance variable initializations in one line if all their names are the same as the parameter names?

private Quiz(int id, String name, int creatorId, Date timeCreated,
        int categoryId, boolean randomOrder, boolean multiPage,
        boolean immediateCorrection, boolean allowPractice) {
    this.id = id;
    this.name = name;
    this.creatorId = creatorId;
    this.timeCreated = timeCreated;
    this.categoryId = categoryId;
    this.randomOrder = randomOrder;
    this.multiPage = multiPage;
    this.immediateCorrection = immediateCorrection;
    this.allowPractice = allowPractice;
}
nemo
  • 11
  • 3
  • I guess there is no way. But I may be wrong. – Rohit Jain Feb 26 '15 at 05:32
  • 2
    are you using spring? – Sanjay Rabari Feb 26 '15 at 05:35
  • 1
    possible duplicate of [Can I obtain method parameter name using Java reflection?](http://stackoverflow.com/questions/2237803/can-i-obtain-method-parameter-name-using-java-reflection) – Nir Alfasi Feb 26 '15 at 05:36
  • @alfasin - I was thinking along similar lines.. but this approach will be terribly slow. :) – TheLostMind Feb 26 '15 at 05:42
  • if you have More number of instance variable then either you can use builder pattern or you can use Telescoping constructor pattern or JavaBeans pattern. as on effective java builder pattern is better to use. **Consider a builder when faced with many constructor parameters** – Prashant Feb 26 '15 at 05:53

4 Answers4

4

Unfortunately there is no simpler way to initialize instance variable - you have to write such initialization code in a constructor.

However all modern IDE (like IntelliJ IDEA, Eclipse, etc.) can generate such constructors automatically based on instance variables, so you don't have to write such code manually. (For instance in IntelliJ IDEA press Alt+Insert, choose Constructor, select variables which you need and the constructor code will be generated).

Also, if you have so many variables which you need to pass and initialize in the constructor (and especially if not all of them are required) - consider to use patter Builder (unfortunately you will have to write even more code!). Here is an example how to implement Builder: http://www.javacodegeeks.com/2013/01/the-builder-pattern-in-practice.html

Igor Uzhviev
  • 308
  • 1
  • 4
2

No there is not, but you should refer to the builder approach since there are a lot of parameters / arguments to the constructor in there.

The builder would make the object creation readable, less error prone and assists in thread safety as well.

Take a look at When would you use the Builder Pattern? for details and samples.

Community
  • 1
  • 1
Khanna111
  • 3,627
  • 1
  • 23
  • 25
  • I disagree.. Builder pattern is usually used for pluggable components inside a class. I see no such thing here – TheLostMind Feb 26 '15 at 05:41
  • Fine, but please take a look at the link as well. All the details are there especially read Mr. Blochs statement excerpted from Effective Java. – Khanna111 Feb 26 '15 at 05:43
1

Project Lombok has a series of class annotations you can add to your class that will generate constructors of the kind you describe. Take a look at @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor here:

https://projectlombok.org/features/index.html

The other annotations available in Lombok are similarly magical for removing boilerplate from your classes, take a look. Lombok is fantastic and IntelliJ Idea has good plugin support for debugging and testing Lombok-annotated classes.

DWoldrich
  • 3,817
  • 1
  • 21
  • 19
0

You can use a factory method that acts like a constructor, but actually returns an anonymous subclass of the main class. The methods of the anonymous subclass can access the factory method parameters as long as they are declared final. So this technique can only be used for fields that never change.

import java.util.Date;

abstract class Quiz{

  static Quiz newQuiz(final int id, final String name, final int creatorId, final Date timeCreated,
                      final int categoryId, final boolean randomOrder, final boolean multiPage,
                      final boolean immediateCorrection, final boolean allowPractice) {

    // Return anonymous subclass of Quiz
    return new Quiz(){

      @Override
      public String someMethod() {
        // Methods can access newQuiz parameters
        return name + creatorId + categoryId + timeCreated;
      }

      @Override
      public boolean someOtherMethod() {
        // Methods can access newQuiz parameters
        return randomOrder && multiPage && allowPractice;
      }

    };
  }

  public abstract String someMethod();
  public abstract boolean someOtherMethod();


  public static void main(String[] args) {
    Quiz quiz = Quiz.newQuiz(111, "Fred", 222, new Date(), 333, false, true, false, true);
    System.out.println(quiz.someMethod());
    System.out.println(quiz.someOtherMethod());
  }

}
Eamonn O'Brien-Strain
  • 3,352
  • 1
  • 23
  • 33