Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the advantage of using private fields in POJO ?  RSS feed

 
Prakash Mani - Attur
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I cannot get the advantage of using private field and public getter and setter POJOs. Why do not we use public field without getter and setter. Whats the problem in it ? Please clarify this.

Thanks
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try searching the web or some introductory books -- look for "encapsulization" as a basic OO software engineering technique.

rc
 
Nico Van Brandt
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he meant encapsulation by that

It is hiding the fields from external classes so that you can only get and set the values by using the getter and setter methods.
You can also do some standard conversions or checks in your getters and setters.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Van Belle wrote:
You can also do some standard conversions or checks in your getters and setters.


what are these conversions and checks ? why would one want to use checks ? can you give some examples/links ?
Is it checks like the one below ???




 
Nico Van Brandt
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example in my mind of a conversion could be with a setter of a score field


By a check I mean a small if statement in the setter to see whether the value you try to set is not out of bounds in your application
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote: . . . Is it checks like the one below ???

Yes, that is a possibility.

Now tell us why that check will always fail.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was pseudocode The code will work incorrectly if it checks whether two references point to the same StringBuffer object or not.
The correct thing to do would be to compare the contents of the SBs by first converting them toString() and then comparing the resulting strings by equals().

PS : i tried comparing the SBs using only equals and it failed. Why ? And i cant even override equals since its final so typing long code is the only way out.
 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private fields help in information hiding and also giving a control over the access of the fields which is nothing but encapsulating the data. With setters you can have a control over the value which is being set for the field. Consider the following example:


You would not want to set a negative age for a person. Suppose you dont want to hold the age value but want to calculate it from the DoB and the current date you could do it the following way (Pseudo code):

 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:

PS : i tried comparing the SBs using only equals and it failed. Why ? And i cant even override equals since its final so typing long code is the only way out.


Any comparison between two references pointing to different instances using "==" operator will always result in false.

PS: I dont want to go off topic to this thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!