Your professor is correct. You do not want to blindly create getters and setters for all instance variables. You want to create them where they are required.
If you have a BankAccount
class with a balance
instance variable, creating getters and setters for it makes sense if you want to be able to check and set the balance.
Even here there is information hiding - in the form of encapsulating the implementation. The getter "double getBalance()
" could simply return the value of the underlying instance variable if it is a double
, or it could be returning a value derived from a BigDecimal
if that is the implementation choice for the variable, or it could be calling a remote web service and returning the result. So, the getter/setter still allows for the implementation to vary and thus does not violate encapsulation (and by extension, neither do JavaBeans).
What JavaBeans do for you is define an interface (getXXX(), setXXX()
) for getting and setting "properties", attributes of a class that users would typically want to examine or change. If your class has information that is not considered a "property", there is no need to expose it. For example, let's say the BankAccount
class has an instance variable used for validating withdrawals. If the client does not need access or modify this, there is no point in creating a getter or setter for it.