Firstly Understand these two examples , then read point below it : Example 1 : Here we are Using Setter Methods to initialize class level variable Example 2: Here we are using Parameterized Constructor to create object and passing values as argument to constructor , the values which we will pass while creating object , those values will get assigned to particular object properties/variables. Constructor is used to create , initialize object. When we use parameterized constructor to pass parameter it only call once. Means you can only pass parameter once. if you call it more then 1 time each time a new object created in memory. While getter and setter can be called according to your use many time to set or get values. In constructor , you can pass variables values as parameters then at object creation time those value will be assigned to instance variables. Where as in setter method : once the object is created then you can use it for setting up some value to it. It a...
Best Articles To Read