What is the Java equivalent of C++’s const member function?

There’s no equivalent to the C const “type modifier” in Java (sadly).

The closest you can get is to return an immutable object or an immutable wrapper around a mutable object.

Immutability is not a language feature of Java, however, so you’ll have to rely on Libraries.

Examples of immutable objects are:

  • the primitive wrappers Integer, Character, ..
  • String
  • File
  • URL

Commonly used immutable wrapper (i.e. wrappers around mutable types that prevent mutation) are those returned by the Collecton.unmodifiable*() methods.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)