1

In C++, one can define a const method:

class MyClass {
    void myMethod() const {
        .. code ..
    };
};

The const here means that this method has only read permissions to this. I.e., it cannot make any change to the object it works on. It can only make read operations on it.

Is there an equivalent in Java for const method?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126

1 Answers1

2

Creating an answer from my comment: There is no equivalent for this in java.

A member function has always write access to the fields of it's class. If you want to change that behavior, you have to move the function outside the class and hand over a copy to it.

ifloop
  • 8,079
  • 2
  • 26
  • 35