Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java :Setter Getter and constructor

I'm a bit confused about the use of getter/setters and constructors (see the below code for an example)

    public class ExampleClass {          private int value = 0;           public ExampleClass () {             value = 0;          }          public ExampleClass (int i) {             this.value = i;         }          public int getValue() {             return value;          }          public void setValue(int val) {             this.value = val;          }          public static void main(String[] args) {                  ExampleClass example = new ExampleClass (20);             example.setValue(20);              //Both lines above do same thing - why use constructor?              System.out.println(example.getvalue());         }    } 

All I've learned is that we need getters/setters for security and that they can also be used to change or edit values later on.

My question is that if the constructor is the point of initialization and a default constructor is always present, why use a constructor with parameters to initialize values instead of getters/setters?. Wouldn't using the getter and setter provide security as well being able to easily change values at any stage. Please clarify this point for me.

like image 956
Java Student Avatar asked Jul 30 '13 09:07

Java Student


People also ask

What is difference between constructor and getter setter in Java?

We can say another way i.e Constructor used for creating an object and setters used for changing the values inside object, getters() user for getting the values, this is only the main difference.

Should I use getters and setters in constructor?

You should not call getters and setters from the constructor. A constructor constructs the specific class in which it is defined. It is its job to initialise the fields because - well - nothing else will. The only way to guarantee initialising the fields is to assign them.

How do you write getters setters and parameterized constructors in Java?

For example the default value of the int is 0. If you want to create a instance with the value of int parameter other than 0, you can use constructor. if you are using setters, they are actualy methods, so if you have more than one setters then it is better to use constructor.

What is the difference between getter and setter methods in Java?

The getter method returns the value of the attribute. The setter method takes a parameter and assigns it to the attribute. Getters and setters allow control over the values. You may validate the given value in the setter before actually setting the value.


1 Answers

default constructor is always there

Well actually its not always there. A default constructor is the one which is provided by the compiler (of course it is a no-arg constructor ) Only if there is no other constructor defined in the class

why we use constructor with parameters to initialize values instead of set get

Because there could be a condition that an object can always be created only when all the values are provided at the time of initialization itself and there is no default value. So all values must be provided otherwise code will not compile.

Consider this Book class

public class Book {      private String title;     private String author;      public Book(String title, String author){         this.title = title;         this.author = author;     }      //getters and setters here  } 

Consider a condition where a book can be created only if it has title and author.

  • You cannot do new Book() because no-arg constructor is absent and compiler will not provide one because one constructor is already defined.
  • Also you cannot do new Book() because our condition does not meet as every book requires a title and author.

This is the condition where parameterized constructor is useful.

like image 82
Prasad Kharkar Avatar answered Oct 14 '22 22:10

Prasad Kharkar