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

  • Home
  • SEARCH
  • 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 8750803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:55:48+00:00 2026-06-13T12:55:48+00:00

We know that the final keyword creates a constant reference to an object. For

  • 0

We know that the final keyword creates a constant reference to an object. For example:

final Object object = new Object();

However, in this case , only the reference to the object is fixed and cannot be changed during the execution runtime of the program. I may still call setter methods on this object and alter the values of its data members. Is there a way I can prevent this in Java (equivalent of const in C++) ?

I understand a way where I should not define setters to this object class or should allow setting of values only once, but what if I want to pass this object as an argument to a function and do not want that function to alter values of this object’s data members. I may still want to alter the values outside the function.

What should be the function prototype in that case?

  • 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-13T12:55:48+00:00Added an answer on June 13, 2026 at 12:55 pm

    The trick is to define a read-only interface to the object, and put the setters only into the class, like this:

    public interface ConstInterface {
        int getFirst();
        String getSecond();
    }
    
    public class MutableClass implements ConstInterface {
        private int _first;
        private String _second;
        int getFirst() {
            return _first;
        }
        void setFirst(int f) {
            _first = f;
        }
        String getSecond() {
            return _second;
        }
        void setSecond(String s) {
            _second = s;
        }
    }
    
    final ConstInterface obj = new MutableClass();
    

    At this point, you can access obj as read-only through its interface. If you need to set properties, cast obj to MutableClass, and call the setters.

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

Sidebar

Related Questions

I know that interface can have only static and final values implemented inside it..
We know that the classic range random function is like this: public static final
If you are familiar with TopCoder you know that your source-code gets a final
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I know that you can create a separate console application, however, I am in
I know that this question might have been asked like 100 times, but, believe
I know that there are a lot of questions and answers exactly about this
When should we use an uninitialized static final variable? I know that an uninitialized

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.