• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what is the advantage of using private fields in POJO ?

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try searching the web or some introductory books -- look for "encapsulization" as a basic OO software engineering technique.

rc
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
No more fooling around. Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic