Should I always use getter/setter methods in Java classes or are there times when its okay to use public attributes?


This question already has an answer here:


Encapsulation is one of the core concepts of object oriented programming. Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will.

Typical example, consider having a dateOfBirth parameter. With a setter and getter you can have a small validation process, making sure that the user was not born in the future, or is impossibly old. You can also use the setter to update some other fields, such as age.

This minor validation can also enhance code re-usability since you do not need to have to make this check in any other class which invokes these getters and setters.

Need Your Help

is there any free api which i can use to load list of countries states cities etc dynamically may be from Google maps

javascript google-maps google-maps-api-3

Hi I want to make a website where in the drop down for countries states cities can be filled by ajax call. But I don't want to keep all these values on my local database. Please suggest possible data

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.