Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Craig Daniels   on Dec 01 In Java Category.

  
Question Answered By: Cheri Garcia   on Dec 01

Oh, it's very nice code to be sure, but it doesn't allow modification
of i. So, let's modify the original class  a bit.

class ConstTest_C{
private string s;

public:
const string* getStr(){return s;};
void setStr(string val){s = val;};
};

So getStr returns a pointer to a const  string. This forces usage of
the setStr function to change s, as getStr's return  cannot be altered.

Now in Java, I can return a reference to the string, but I cannot make
it so that the string pointed to is treated as a const/final String.
Note that

public final String getStr(){return s;}

makes the method final (non-overridable), not the String reference.

So, given this information, is there a way in Java to return a
reference to an object so that the object cannot be altered?

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on returning const Or get search suggestion and latest updates.


Tagged: