Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

returning const

  Asked By: Craig    Date: Dec 01    Category: Java    Views: 478

I know in C/C++, you can return const values so that you can obey OOP,
but keep data integrity like so:

class ConstTest{
int i;

const int getIConst(){return i;}

is there any way to do this in Java?



7 Answers Found

Answer #1    Answered By: Randy Warren     Answered On: Dec 01

In Java, const  variables are called final variables.

But, using the right get() methods, no one should ever be able to
access your private variables, so I haven't seen final being used
that much. Of course, I'm relatively new to Java, so I don't know
that much anyway. ;)

Answer #2    Answered By: Frederick Greene     Answered On: Dec 01

In theory final objects will be more effecient to load up.

Answer #3    Answered By: Kelly Bell     Answered On: Dec 01

I think you are talking of immutable objects to be returned from the
function like we have in C/C++.

Answer #4    Answered By: Angel Harris     Answered On: Dec 01

class ConstTest{
public static final int  Const_I=2;

what is the problem with the code above, don't u like it?

Answer #5    Answered By: Cheri Garcia     Answered 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;

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?

Answer #6    Answered By: Julian Long     Answered 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:


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).

Answer #7    Answered By: Omar Walker     Answered On: Dec 01

As you have probably figured out the answer is no.
But you can force the client of your class  to not
change the underlying object that you return. For
example :

class ConstTest_C{
private String s;

public String getString()
if ( this.s == null )
return null;
return this.s.clone();

public void setString( String s ) { this.s = s; }

The user of your class can do anything they want with
the String that is returned by getString(), but it
will not change the class's String object.

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