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

后端 未结 9 548
长发绾君心
长发绾君心 2021-01-01 10:23

In C++, I can define an accessor member function that returns the value of (or reference to) a private data member, such that the caller cannot modify that private

相关标签:
9条回答
  • 2021-01-01 11:05

    One way to avoid this issue is to no expose the data structure (another is returning a copy, or an immutable wrapper)

    e.g. instead of

    public List<String> getSomeList();
    

    have

    public String getSomeElement(int index);
    
    0 讨论(0)
  • 2021-01-01 11:10

    This does not exist in java. final and const have different semantics, except when applied to a variable of a primitive type. The java solution typically involves creating immutable classes - where objects are initialized in construction and provide no accessors allowing change. Example of such classes would be e.g. String or Integer.

    0 讨论(0)
  • 2021-01-01 11:10

    You haven't overlooked anything. There is no way in pure Java to do so. There might be libraries which provide some subset of this using annotations, but I don't know any offhand.

    The way you pass back a reference to immutable data is to make the class you pass back immutable, plain and simple. There are a couple of library functions to help you produce an immutable view of some data in some very limited but common cases. Here's one example:

    private List<String> internalData;
    
    public List<String> getSomeList() {
        return Collections.unmodifiableList(internalData);
    }
    
    0 讨论(0)
提交回复
热议问题