DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Controller and Data Access: Understanding Two-Layer Architecture Through Abstraction and Encapsulation
  • Best Ways to Write Clean and Quality Python Code
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues

Trending

  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • RAG Architecture Patterns: Proxy, Caching, and Routing
  • Apache Phoenix: Global Secondary Indexes With Tunable Consistency
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Java’s Encapsulation - When the Getter and Setter Became Your Enemy

We know that we should reduce the visibility of the field as much as possible, but it will be a waste if you have a data breach when we put getter and setter to all methods as public.

By 
Otavio Santana
DZone Core CORE ·
Jun. 29, 22 · Presentation
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Encapsulation is the key to a good code design, but how to archive it? Java developers automatically think about putting getter and setter in a private attribute. 

The encapsulation is a wrapping up of data under a single unit. It is the way to protect the data with OOP and avoid any security issues. The main idea around encapsulation is the data's guardian or a shield from being accessed by the code outside this shield when it makes sense. 

In the clean code, we have Uncle bob when he talks about the significant difference between structure code from OOP is the OOP you hide the data to expose the behavior.

Using a good encapsulation, we have several advantages: 

  • Data Hiding
  • Increased Flexibility
  • Reusability
  • Testing code is easy

We know encapsulation is essential from those points and many more aspects we did not put in this article. The challenge of this topic is how to apply it. 

We usually think about getter and setter as the primary way to archive it; however, sometimes, those methods affect the model, doing the same result as a public field, so de-encapsulation the entity.

Nevertheless, it is the same path to either POJO or the anemic model, making us go away from good code. 

We know that we should reduce the visibility of the field as much as possible where the higher priority is the private, but it will be a waste if you have a data breach when we put getter and setter to all methods as public.

Join us today and understand why blindly applying getter and setter are the worst solutions to encapsulation and what you should use instead. 

Encapsulation (networking)

Opinions expressed by DZone contributors are their own.

Related

  • Controller and Data Access: Understanding Two-Layer Architecture Through Abstraction and Encapsulation
  • Best Ways to Write Clean and Quality Python Code
  • Unraveling Lombok's Code Design Pitfalls: Exploring Encapsulation Issues

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook