Reputation: 23
please, dont try to kill me with all the "only accessors are right" talk... I came in peace :)
I just kindly want to ask, what is the main reason, that Java Beans MUST use accessors even in situations only simple attribute access is required. I know accessors are good thing, in certain situations. But, as my favourite article says, "dont use accessors, unless you are 100% certain you need more than simple atribute access, they are waste of CPU and programmer time".
Again, please, dont attack me for not using accessors all the time. I only ask, what is the reason it HAS to be used in order to use and create proper Java Bean. Why people at Oracle said lets do it this way... Thanks.
Upvotes: 2
Views: 754
Reputation: 116620
While this is a requirement for original Java Beans, as per convention, it is worth noting that many libraries that support "beans" do also consider public fields as valid accessors; and usually also allow use of annotations to specify non-public methods and fields as well. This includes libraries like JAXB (for XML-to-POJO data binding) and Jackson (JSON-to-POJO).
So in some ways original Java bean 'specification' (convention as documented in JDK javadocs) is not all that relevant any more.
I assume original authors of convention just felt that direct field access was not a good practice. Personally I think there are cases where simple immutable struct-like classes are fine, and it's pretty silly to have to write monkey code for getters (or setters for mutable ones); but there is fine line between "Java structs" (just data, no logic or at most very little) and "real" objects (state and behavior).
Upvotes: 1
Reputation: 1266
The main reason of using accessors it to have a full control over its modifications within a single place.
I could find many more reasons ... :)
IMHO using accessors and mutators gives as a way to make our code more extensible and much more flexible
Upvotes: 2
Reputation: 6500
If you need to hide the details of the property, then you will need accessors. i.e for example there is a computed value (say balanceDue) which is computed from two other actual properties (say credit and debit), then you can have code in your accessors that do the computing. Otherwise, you will need to do teh computation within every client that invokes the individual fields. This is one example where accessors make sense from a logical point of view. Basically, encapsulation.
Upvotes: 0
Reputation: 598
By using accessors you can apply logic when you get/set a value. It isolates the bean from the rest of the application.
If you need to alter the internal storage of the bean, you can do that, and still keep the same getters/settes.
For example: If you have a class for storing an ip address, you might wan't to store it as an Long value instead of a String (for effiecency). You can do this by only make changes to one class.
Also take a look at GRASP, especially Information Expert
Upvotes: 0
Reputation: 81667
In fact, a Java bean is a bean if it follows the conventions:
So it's just a matter of conventions...
Upvotes: 1
Reputation: 1430
Java beans are a convention. Frameworks that work with Java beans rely on the details of that convention to reliably manipulate objects.
There's actually a good explanation on wikipedia: http://en.wikipedia.org/wiki/JavaBean
Upvotes: 3