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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:34:56+00:00 2026-05-25T12:34:56+00:00

In C++ a getter & setter for a private data member is very useful

  • 0

In C++ a getter & setter for a private data member is very useful due to the ability to control mutability via a const return value.

In Java, if I understand correctly (please correct me if I am mistaken), specifying final on a getter doesn’t work that way. Once the caller received the data member reference through the getter, it can modify it, despite it being private…

If that’s the case (and please correct me if I have a gross misconception here), why not declare the data member public and simplify things?

  • 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-25T12:34:56+00:00Added an answer on May 25, 2026 at 12:34 pm

    Making immutable return values in java is a matter of either returning already immutable objects types (such as String) or returning a copy for non-immutable objects.


    Sample 1 – Already immutable object

    public String getValue() {
        return value;
    }
    

    Sample 2 – Collection of already immutable objects

    public List<String> getValues() {
        return new ArrayList<String>(values);
    }
    

    Sample 3 – Non-immutable object

    public Complex getComplex() {
        return complex.clone();
    }
    

    Sample 4 – Collection of non-immutable objects

    public List<Complex> getComplex() {
        List<Complex> copy = new ArrayList<Complex>(complexs.size());
        for (Complex c : complexs) 
            copy.add(c.clone());
        return copy;
    }
    

    Sample 3 and 4 are for conveniance based on that the complex type implements the Cloneable interface.

    Furthermore, to avoid subclasses overriding your immutable methods you can declare them final. As a side note, the builder pattern is typically useful for constructing immutable objects.

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

Sidebar

Related Questions

Imagine the following template class (setter and getter for the member _t omitted): template<class
I'm generating compiled getter methods at runtime for a given member. Right now, my
I am using a magic getter/setter class for my session variables, but I don't
When a class field is accessed via a getter method by multiple threads, how
I would like to combine setter/getter in one method, in C++, in order to
Imagine you have a class with dozens of private member variables. Each member variable
Given the following classes: public class Container { private List<SomeData> list; // public getter
I have the following code: template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()> struct
When a getter returns a property, such as returning a List of other related
How to write a getter that can not be deleted? I want to own

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.