-1

I am new to java so Please help me. I was searching about the usage of getter and setter and it was when i was confused on the working of setter and parameterized constructor.

parameterized constructor is also used to access private attributes of class, isn't it?

1 Answers1

-1

You'd need to use parameterized objects anytime you want to pass any sort of configuration into your object. You could use setters to pass in that information later, but not only is it shorter and cleaner to pass that information in at construction time, it lines up with the good practice of creating immutable objects, which cannot be modified after construction and have no setters. 2nd use case is that, if you want to force for a mandatory attribute of a class, go for the parameterized constructor. For example,

class Student {
  private final String name;
  private final Date birthday;
  public Student(String name, Date birthday) {
    this.name = name;
    this.birthday = birthday;
  }
}  
Dinesh
  • 1,046
  • 1
  • 8
  • 17