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: Julian Long   on Dec 01

Not sure this helps but this is the first thing that comes to mind.

the example below wont allow modification of s.

If you did:

String x = ConstTest.getStr();
x = "Xyz";

ConstTest's private value wouldn't be altered.

This is the common rule: you can ONLY alter objects by calling methods on
them. Not by changing them via the = sign. If we could do:

x.setValue("Xyz");

we'd change the internal object in ConstTest. (Part from the fact that
Strings still are returned by value in java, not by reference. at least I
think so...)

On the opposite: To make it possible to return  a string that, when altered
would affect the object in ConstTest you'd need to use StringBuffer instead.
(Or wrap your string in another object that you use getters and setters on).

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: