Why getter and setter are better than public fields in Java
public modifier vs getter and setter method in Java
Read more �
Providing getter and setter method for accessing any field of class in Java may look unnecessary and trivial at first place, simply because you can make field publicand it�s accessible from everywhere in Java program. In fact many programmers do this in there early days but once you start thinking in terms of enterprise application or production code, you will start seeing how much trouble it can create in terms of maintenance. Since as per SDLC process, software spends more time in maintenance than development, it�s worth keeping ease of maintenance as one of goal in development. In reality using getter and setter method in Java is one of the Java coding best practice much like using @Override annotation while overriding method in Java. Main problem with making field public instead of getter and setter is that it violates Encapsulationby exposing internals of a class. Once you exposed internals of class you can not change internal representation or make it better until making change in all client code. Since every code change comes with risk and cost of regression testing is high during maintenance, its not a good idea to make field public in Java. In this Java tutorial we will see some more benefits getter and setter offers over public fields in Java.
Comments
Post a Comment
-Berkomentarlah yang baik dan rapi.
-Menggunakan link aktif akan dihapus.