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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:57:38+00:00 2026-05-23T00:57:38+00:00

class Foo { String bar; // private String bar; } public class A {

  • 0
class Foo {
    String bar;
    // private String bar;
}

public class A {
    public void method1() {
        Foo foo = new Foo();
        // foo.bar; or foo.getBar(); which is preferable

    }
}

Does it make sense to declare variables as private with getters/setters for class member definitions (e.g. should bar be declared as private in Foo or should it be just declared as String bar)? I would prefer to remove them, sense they cannot be accessed outside this class anyway. What would be preferable?

  • 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-05-23T00:57:39+00:00Added an answer on May 23, 2026 at 12:57 am

    Using getters and settings to access/mutate a private field member is standard OO practice. It allows the inner workings of the class to change while maintaining its public interface.

    If you were to access/mutate bar by making it directly available as part of the public interface (by making it public) it may seem fine now but what happens when, months later, you decide you want to add some validation to bar? Maybe you don’t want bar to ever be set to null. How could you achieve that? If you use getters and setters, all you would need to do is change your setter to throw an exception if an attempt is made to set bar to null. If you mutate bar directly, you couldn’t do that. More drastically you may decide that bar is no longer required at all!

    Getters and setters do not imply that a field member of the same name and type exists internally of the class. They tell you only that the class has a property that can be accessed/mutated and dictate nothing about the way in which the class performs that internally. This gives you a great deal of flexibility.

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

Sidebar

Related Questions

Let's say I have a class: class Foo { public string Bar { get
Suppose you have a collection of Foo classes: class Foo { public string Bar;
I'm used to writing classes like this: public class foo { private string mBar
class Foo(){ public List<string> SomeCollection; } I need to implement an event which can
Consider this class: public class Foo { // Fields private string _bar; // Properties
If i have a private class Class A { public static string foo; }
public class foo { private String _name; private String _bar; public String getName() {
Consider the following class hierarchy: public class Foo { public string Name { get;
For example, suppose I have a class: class Foo { public: std::string& Name() {
How do I call start() below? package com.example.test; class Bar {} public class Foo<K>

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.