Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:41:01+00:00 2026-06-16T18:41:01+00:00

In my project I have a small data structure Key . public class Key

  • 0

In my project I have a small data structure Key.

public class Key implements Serializable {

  private static final long serialVersionUID = 1L;

  public String db;
  public String ref;
  public Object id;

  protected Key() {
  }

  public Key(String db, String ref, Object id) {
    this.db = db;
    this.ref = ref;
    this.id = id;
  }
}

Yes this class is simple and every field is publicly accessible.

But someone has suggested I use POJO style classes instead but when I asked why they were unable to tell me.

In my opinion , calling getters and setters is slower than direct access to a field.

So why I must use POJO programming style?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:41:02+00:00Added an answer on June 16, 2026 at 6:41 pm

    Taken from Wikipedia:

    POJO is an acronym for Plain Old Java Object. The name is used to
    emphasize that a given object is an ordinary Java Object, not a
    special object.

    A POJO is usually simple so won’t depend on other libraries, interfaces or annotations. This increases the chance that this can be reused in multiple project types (web, desktop, console etc).

    As someone has already pointed out in the comments, your object is technically a POJO already however you have specifically asked about getters and setters which are more akin to JavaBeans.

    There are a number of reasons I can think of for using getters and setters:

    1. You might only want to get some of the values (I.E. read only values). With fields, clients can both get and set the values directly. Fields can be made read-only if they are marked as final although this doesn’t always guarantee that they are immutable (see point 9).
    2. Getter & setter methods allow you to change the underlying data type without breaking the public interface of your class which makes it (and your application) more robust and resilient to changes.
    3. You might want to call some other code such as raising a notification when the value is obtained or changed. This is not possible with your current class.
    4. You are exposing the implementation of your class which could be a security risk in some cases.
    5. Java beans are designed around POJO’s which means that if your class is not implemented as one it can’t be used by certain tools and libraries that expect your class to adhere to these well established principles.
    6. You can expose values that are not backed by a field I.E. calculated values such as getFullName() which is a concatenation of getFirstName() and getLastName() which are backed by fields.
    7. You can add validation to your setter methods to ensure that the values being passed are correct. This ensures that your class is always in a valid state.
    8. You can set a breakpoint in your getters and setters so that you can debug your code when the values are obtained or changed.
    9. If the field is an object (I.E. not a primitive type) then the internal state of your class can be modified by other objects which can lead to bugs or security risks. You can protect against this scenario in your POJO’s getter by returning a copy of the object so that clients can work with the data without affecting the state of your object. Note that having a final field does not always protect you against this sort of attack as clients can still make changes to the object being referenced (providing that object is itself mutable) you just cannot point the field at a different reference once it has been set.

    Yes, accessing or setting the values via method calls may be slower than direct field access but the difference is barely noticeable and it certainly won’t be the bottleneck in your program.

    Whilst the advantages are clear this does not mean that getters and setters are a silver bullet. There are a number of ‘gotchas’ to consider when designing real world, robust scalable classes.

    This answer to a very similar question looks at some considerations in detail when designing a class that has getters and setters. Although the suggestions may be more relevant depending on the type of class you are designing E.G. a class that forms part of an API in a large system as opposed to a simple data transfer object.

    Also note that there may be certain scenarios where a class with direct field may be advantageous such as when speed is essential or memory is limited although this should only be considered after profiling your code and finding that it is actually a bottleneck.

    Also be careful that you are not just wrapping all of your fields in getters and setters as this is really missing the point of encapsulation.

    This answer provides a good summary of the reasons for choosing a POJO over a JavaBean style object with getters and setters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a small old project in Net.data where some development is still occurring.
I have small problem. Code behind: ... public struct Project { string Name; string
I have a small project which stores data in IndexedDB in browser. I would
I have a small project that I was using node-dirty for, but it's not
I have done small project in c++, the IDE I am using is Visual
I have a small project that I will be working on shortly that collects
I have some small project to stream video to android device. Streaming is done
I have a small project and I noticed that I have a lot of
I have a small software project with a couple clients. What I'm looking for
I have a small iPhone Project with a UITextView on my View, designed in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.