0

If there is const at the end of a method declaration in C++, it means you can not change any member data. Because this pointer would be const. Is there something like in Java? If not, any similar way to prevent alteration of data member?

#include <iostream>

class MyClass
{
private:
    int value_;
public:
    void Foo() const
    {
        value_ = 123;
    }
};

Probably the error message occurs, assignment of member 'MyClass::value_' in read-only object

  • 2
    I'm afraid it doesn't exist in Java, and that's a shame. – Jean-François Fabre Nov 23 '16 at 19:41
  • Closest match might be the `final` keyword. Although I don't know the implications of `const` very well. – Marvin Nov 23 '16 at 19:43
  • how come? Could you explain? @Marvin – Soner from The Ottoman Empire Nov 23 '16 at 19:43
  • @snr: https://en.wikipedia.org/wiki/Final_(Java) (probably can't explain it better; in short: it means you can only assign a variable once, effectively making it read-only from then on). – Marvin Nov 23 '16 at 19:44
  • There is nothing like that in Java. You might want to use immutable objects with R/O-semantics to pretend a `const`-safe object, or use R/O-interfaces that declare read-only methods to accent the R/O semantics, so you could not worry about passing such objects to respective methods that would just accept such R/O-interface-d parameters. – Lyubomyr Shaydariv Nov 23 '16 at 19:50
  • One implication of `final` is that you need to guarantee it can only be set once (e.g. upon declaration or in the constructor). You cannot "mistakenly" initialize it in a regular setter. – Marvin Nov 23 '16 at 19:51
  • 1
    Related: [Why is there no Constant feature in Java?](http://stackoverflow.com/q/2735736/10077) – Fred Larson Nov 23 '16 at 19:52
  • related but I don't think as duplication. @FredLarson – Soner from The Ottoman Empire Nov 23 '16 at 19:58

1 Answers1

0

The java equivalent is:

class MyClass {
    private final int value_; // must be assigned at construction

    public MyClass() {
        value_ = 42; // must assign a value
    }

    public void foo() {
        value_ = 123; // compile error if you try to assign to a final field
    }
};
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • yes, _similar equivalent._ What if I want to change the object variable in another member method? I think there is no way to do so. Hence, it makes the answer the closest one for the question. – Soner from The Ottoman Empire Nov 23 '16 at 19:52
  • @snr there is no way. `final` is *final*. You can make the method `final` to prevent subclasses from overriding it, if that helps. Other than that, you would have to design your class to allow certain state changes internally, but disallow external code from changing state. AFAICT there's no built-in way to support what (I think) you want to do. – Bohemian Nov 23 '16 at 19:55
  • Thanks for your interest. I like getting response from connoisseur people as you to new(young) ones as me. – Soner from The Ottoman Empire Nov 23 '16 at 19:59