Equivalent of const(C++) in Java

42
January 11, 2019, at 11:00 AM

I was wondering if there was an equivalent to c++'s const in Java. I understand the final keyword, but unfortunately I cannot use that to declare a functions return value final. Instead, it always ensures the function cannot be overridden, correct?

Basically, I want to make sure a given returned class cannot be modified and is read only. Is that possible in Java?

Answer 1

Basically, I want to make sure a given returned class cannot be modified and is read only. Is that possible in Java?

Not directly, but one workaround is an immutable object.

Example -

public final Foo{
    private final String s;
    public Foo(String s){
        this.s = s;
    }
    // Only provide an accessor!
    public String getString(){
        return s;
    }
}
Answer 2

Java does not support const-qualification of references to objects.

Workarounds include the following:

  • Immutable objects.
  • Unmodifiable views of modifiable objects (for example, those returned by Collections.unmodifiable*()).
  • Read-only interface types
  • Defensive copies.

Immutable objects prevent modification by the recipient, but they also prevent modification through any reference. They are not equivalent to C++ const pointers/references.

Closest are read-only interface types. You could map a const-correct C++ class to two Java units, one an interface with only the const-qualified methods.

This is sometimes useful, to control what can modify an instance of the class. However, it's not done as frequently as in C++.

Answer 3

The "final" keyword means const. But the use of final in java is much different than C++ Only fields or local variables can be final. A final class is not a const class but a sealed class that cannot be inherited. A final method is a sealed method, a method that cannot be overridden.

In C++ there is the concept of "const type" but in java you have only the concept of const field, const method (not-overridable) and const class (not-inheritable).

Answer 4

No

Java does not support the concept of constness as C/C++ use it.

The Java equivalent to this Foo class cannot be written.

class Foo
{
public:
   std::vector<Bar>& bars( void ) const
   { return m_bars; }
private:
   std::vector<Bar> m_bars;
};

However, Java has another approach, using interfaces.

class Foo
{
public:
   ReadonlyList<Bar> bars( )
   { return m_bars; }
private:
   ArrayList<Bar> m_bars;
};

Java's final keyword is more similar to Foo * const than any other c++ concept. Note that Foo const* is very common and useful, while Foo * const is less so.

Answer 5

I think you want a immutable object. An object is considered immutable if its state cannot change after it is constructed.

To do that you need

  • ensure the class cannot be overridden - make the class final, or use static factories and keep constructors private
  • make fields private and final force callers to construct an object completely in a single step, instead of using a no-argument constructor combined with subsequent calls to setXXX methods
  • do not provide any methods which can change the state of the object in any way - not just setXXX methods, but any method which can change state.

    public final class SomeThing{

    public Something(String name) {
        fname=name
    }
    private final String fName;
    public String getName() {
          return fName;
    }
    

    }

Answer 6

Java specifically discarded some features of C++, const is one of it. This conscious decision can be reflected by the fact that const is a reserved word in Java (but not used)

The designers probably thought that the feature is really annoying and not really useful. I had that feeling when I was a C++ coder. Certain kind of static checking is just way too much.

READ ALSO
How to resolve java.lang.ClassNotFoundException: DocumentException

How to resolve java.lang.ClassNotFoundException: DocumentException

I am using Spring Boot with spring security application, i am getting below exception while starting the application

62
BCrypt updating the library and interations - would it break my login?

BCrypt updating the library and interations - would it break my login?

I’ve an old legacy application using Spring Security (31

71
How would you insert one node into another at the treepath?

How would you insert one node into another at the treepath?

I have two nodes and one TreePath in Java SwingOne node contains the 'root' node (let's call this nodeA), it contains everything under it

62