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 7717997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:03:09+00:00 2026-06-01T03:03:09+00:00

It is considered best practice for encapsulation to use private fields with accessors (getters

  • 0

It is considered best practice for encapsulation to use private fields with accessors (getters and setters), instead of protected and public fields.

So, by following this best practice, we never use protected and public anymore. Have they become useless, or else what are their use cases?

The only thing I can think of is for public static final attributes (i.e. class constants).

Note: this is the case at least strongly in the Java world, but the question stands for all languages.

  • 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-01T03:03:12+00:00Added an answer on June 1, 2026 at 3:03 am

    Best practices can change over time.
    I can think in two use cases for public fields, both somehow controversial.
    Adam Bien says in this post that if you are using a DTO, you may not use private + getter + setter but only public fields. This way you are sure that data is transported as is, without any changes. But in the same line he adds that this will cost you lots of meeting explaining why you do it that way…

    Another use for non constant public fields is using public final fields (initialized in constructor) to ensure immutability. Making your classes like

    public Person{
        public final String lastName;
        public final String firstName;
        public Person(String firstName, String lastName){
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
    

    is some sort of a new best practice, advised in places like codemonkeyism.

    But unless you are the absolute owner of the code and/or you can force new standards to be fulfilled, you should keep avoiding the use of public/protected fields…

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

Sidebar

Related Questions

which of the following syntax is considered best practice? For<IMyInterface>().LifecycleIs(new HybridLifecycle()).Use<MyImplementation>(); For<IMyInterface>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.Hybrid)).Use<MyImplementation>(); if the
What's considered best practice when it comes to accessors/mutators and shallow/deep-copy? Or is this
what is considered best practice in the following snippet: int foo(struct data *bar, struct
Not sure if this is considered best practice or if you should even do
In an ActiveRecord model is it considered best practice/necessary to use validates_presence_of when also
What would be considered best practice in this case. I've got a class that's
Would it be considered best practice to use a single template that changes it's
Is it considered best practice to use file appenders or database appenders in log4net?
What is considered best practice deciding how to define the set of JAR's for
Is there a way in Cocoa that is currently considered best practice for creating

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.