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

The Archive Base Latest Questions

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

class X { String x1; String x2; //… String x15; public X (Other o)

  • 0
class X
{
  String x1;
  String x2;
  //...
  String x15;

  public X (Other o)
  {
    // initialize x1,x2,...,x15
  }

  String getx1 () { return x1; }  // lot of getters
  String getx2 () { return x2; }
  //...
  String getx15 () { return x15; }
}

The object of class X is initialized in constructor of class Other;

class Other
{
  private X obj;
  // Other members
  public Other ()
  {
    obj = new X(this);
    //...
  }
  //...
}

I want to somehow get rid of lot of getter methods in class X. I can simply remove those and make all the data members of X in default scope (X is used only within package).

Is there any way by which I can make the members of X obj un-mutable after the constructor Other is called ? Here the idea is, when using get() methods, the variables inside X cannot be modified.

obj.getx1();

Now, if I use simply obj.x1 then there are chances that it might get overwritten (or at least in code review, it will be objected).

[Note: obj has to be initialized in the Other() constructor only].

  • 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-23T07:38:03+00:00Added an answer on May 23, 2026 at 7:38 am

    Best way to make a class immutable (not un-mutable) is to mark all fields as final (visibility is irrelevant, can be public). Fields must be initialized either with an initializer:

    class A { final int x = 10; }
    

    or in every constructor:

    class A { 
       final int x, y, z;
       A(int x, int y, int z) { this.x = x;, this.y = y; this.z = z; }
       A() { this (0, 0, 0); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static T Get<T>() where T : class { string implName = Program.Settings[typeof(T).Name].ToString(); object
class String { private: char* rep; public: String (const char*); void toUpper() const; };
How does the code looks that would create an object of class: string myClass
I'm getting Error Type: Server object, ASP 0177 (0x800401F3) Invalid class string /ErrorProcess.asp, line
The following code doesn't work: class String{ public: char* str; int* counter; String(){ str
Let's say I have a class: class String { public: String(char *str); }; And
In Flex, say I have a Class object. How do I get a string
I have a Class1 public class Class1 { public Class(String s, int[] s1, int
I am able to run the following example code //main class String a=Menu; Object
I've got following class and template function: template <size_t num> class String{ public: char

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.