0

In C++ you could write

Int getX() const { return x; }

Is there an equivalent method structure in Java using final?

What about passing const/ final modified arguments to methods?

Like this C++ code

void printX(const int x); 
StillLearningToCode
  • 2,271
  • 4
  • 27
  • 46

1 Answers1

2

For the C++ example:

void printX(const int x);

you can use final modifier in method parameters to indicate the parameter can't be modified inside the method:

void printX(final int x) {
    System.out.println(x);
    x++; //compiler error since `x` is marked as final
}

Note that using final modifier for object reference variables just means that the reference can't be modified, but its inner contents still can:

class Foo {
    int x = 0;
}
class Bar {
    changeFoo(final Foo foo) {
        foo.x = foo.x + 1; //allowed even if `foo` is marked as final
        foo = new Foo(); //compiler error...
    }
}

From SJuan76 comment for this code

Int getX() const { return x; }

it tells that the class state is not modified when this method is called.

There's no way in Java to mark a method like this.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • If you pass `final List ints` - it can't be modified? – dantuch Jun 01 '13 at 22:43
  • That would be equivalent to `printx (const int x) {...`, not what the the OP is asking for. – SJuan76 Jun 01 '13 at 22:43
  • @dantuch `ints` can't be modified i.e. `ints = new ArrayList();`, but its content can. – Luiggi Mendoza Jun 01 '13 at 22:43
  • @SJuan76 OP's question is pretty ambiguous to begin with. – Luiggi Mendoza Jun 01 '13 at 22:44
  • @LuiggiMendoza You could add something about the use of `final` on methods in java... – A4L Jun 01 '13 at 22:47
  • 1
    @Luiggi Mendoza when you say `parameter can't be modified inside the method` it's true only for immutables (and privities, which are immutable) because the only thing that cannot change is the reference to passed parameter - its internal state can be changed if it's possible. – dantuch Jun 01 '13 at 22:48
  • @dantuch yes, I know this. Answer updated for clarifying this. – Luiggi Mendoza Jun 01 '13 at 22:49
  • @LuiggiMendoza what about `static`? It doesn't prevent static members from being changed but essentially since there is no instance it is under certain conditions stateless? – zsawyer Jun 01 '13 at 22:50
  • @zsawyer `static` modifier in Java can be understood as *it belongs to the class, not to the object reference*. Based on this premise, a getter method should not be `static` at least that the field to retrieve is also `static`. – Luiggi Mendoza Jun 01 '13 at 22:51
  • I agree with Luiggi that there seems to be no way to do that in Java. It is one of the few C++ features that I found more useful than in Java (there are a couple more in C#) – SJuan76 Jun 01 '13 at 22:54
  • [Good reading](http://stackoverflow.com/q/117293/1497596) on whether it is considered a good practice to declare parameters passed by **value** (copied) as `const`. – DavidRR Jun 02 '13 at 02:46
  • @DavidRR yes but this applies more for C++. In Java usually this won't be a problem since [Java is pass by value](http://stackoverflow.com/q/40480/1065197). – Luiggi Mendoza Jun 02 '13 at 04:38