Java :Setter Getter and constructor

前端 未结 11 1354
陌清茗
陌清茗 2020-12-25 12:31

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

    public class ExampleClass {

        private in         


        
11条回答
  •  死守一世寂寞
    2020-12-25 12:56

    Constructor with arguments makes you get the object fully constructed. If you want to use default one, you will have to make sure the fields are set using setters. During set of some property, assume the exception is thrown, now you have an object which is not usable. In several cases, setter wouldn't be exposed but getters. In those cases, having constructor with arguments or a named constructor is the right option. In a nutshell, getters and setters do have their own importance rather than initializing the object.

    Why use getters and setters?

提交回复
热议问题