Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In Java, there is the final keyword in lieu of the const keyword in C and C++.

In the latter languages there are mutable and immutable methods such as stated in the answer by Johannes Schaub - litb to the question How many and which are the uses of “const” in C++?

Use const to tell others methods won't change the logical state of this object.

struct SmartPtr {
    int getCopies() const { return mCopiesMade; }
}ptr1;

...

int var = ptr.getCopies(); // returns mCopiesMade and is specified that to not modify objects state.

How is this performed in Java?

share|improve this question
6  
Please improve your accept rate. –  Jay Nov 12 '12 at 1:16
1  
Note that D language allows the following syntax: int getA(POD o) pure nothrow { return o.a; } which is very cool, IMO. –  Job Nov 12 '12 at 4:04
add comment

4 Answers

up vote 6 down vote accepted

You can't. I'm not familiar with Java 7, but at least in Java 6 you cannot tell the compiler a method is not supposed to mutate its arguments or the this instance.

Unfortunately final in Java doesn't mean the same as const in C++.

A final argument means something else: merely that you cannot reassign it, so that the following is an error:

A method(final B arg) {
    ...
    arg = something;  // error, since arg is final
    ...
}

This is a good practice, but it won't prevent you from mutating arg by calling one of its methods.

A final method such as final A method(...) is related to subclassing rules, and not with mutation.

share|improve this answer
    
Thank you! I can't remember how, with in C, informing the compiler it won't mutate it's arguments will speed or optimize the program; that can be researched on my own. Even with, in your example, variable arg of type B being final, there is no optimization -- Is this because the program is compiled and then interpreted towards the platform running the program? –  Chris Okyen Nov 12 '12 at 3:41
    
@ChrisOkyen: Java demands more from its compiler and runtime. For example if the runtime wants to optimize that methods it should figure out if the method mutates any fields (and it can!). Also note that the compiler (javac) does little to no optimization in Java, that's left to the JVM (runtime). –  Joachim Sauer Nov 12 '12 at 7:42
    
"This is a good practice, but it won't prevent you from mutating arg by calling one of its methods.": In C++ if you have an argument of type const boost::shared_ptr<T>& you cannot change the pointer, but you can still change the object, if the object has non const methods. –  Giorgio Nov 12 '12 at 10:39
    
@Giorgio Thanks, good point. I'm a bit rusty with my C++ :) –  Andres F. Nov 12 '12 at 13:03
    
@Giorgio true, it is easy to mix up immutable const reference/pointer with a variable who's value is not mutable. –  Chris Okyen Nov 12 '12 at 19:22
add comment

Java's final is not in lieu of C's const. In Java final may appear in five places. Attribute, variable and formal parameter declarations and class and method definitions. Using final for attributes, variables or parameters means, you cannot reassign them, so the following will not be accepted by the compiler:

final List<String> xs = new ArrayList<>();
xs = new LinkedList<>(); //illegal

However, note that you can still change the object referenced by xs:

final List<String> xs = new ArrayList<>();
xs.add("Hello World");

As for classes, final forbids inheriting from this class. So, this is illegal too:

final class A {
}

class B extends A { //illegal
}

And for methods final forbids overriding the method in a subclass, thus the following is illegal.

class A {
    final void m() {}
}

class B extends A {
    final void m() { System.exit(1); } //illegal
}

So why would anyone use one keyword for three different intentions? Well, they are connected. If you take a look at java.lang.String you will see, that it is declared as final. And since there are not operations in String that will change the value of the instance you can safely assume that

final String GREETING = "Hello, World!";

defines a constant.

BTW, const is a reserved word in Java, too. But it has no semantics and thus cannot be used in any Java program.

share|improve this answer
add comment

The only way you can make a Java object immutable is from within the class of that object itself and not from other calling classes.

You may only have constant pointer (final reference by the official Java name) meaning that you cannot modify the reference to point to another object; you cannot have pointer to constant, meaning you cannot modify the referring object, as you may have in C/C++.

share|improve this answer
add comment

The only way to create const method in Java is to create a const interface for the object. Like:

interface ConstSomething {
    int getCopies();
}
class Something implements ConstSomething {
    int getCopies() { return mCopiesMade; }
}

Yes, unlike C++ const this affects performance, because now the method has to be called virtually.

Note, that the same applies to C#.

share|improve this answer
    
You are saying that becuase it can't tell the compiler that the method will not modify the objects state affects performance? Why does it have to be called virtually? –  Chris Okyen Nov 12 '12 at 19:27
    
@ChrisOkyen: Because you can't tell the compiler that the method will not modify the objects state you have to call it on an interface if you want to do that anyway. But if you call it on an interface, the compiler does not know the actual type and can't call the method non-virtually. Plus of course it can't do any optimization based on that promise if you can't tell it (because const in C++ only applies to that reference, the room for optimization based on it is rather limited anyway). –  Jan Hudec Nov 13 '12 at 7:38
    
I am not 100 percent clear on how using an inteface and not knwoing the type relates becuase I don't fully understand how in C++ such a method optimizes.. I don't understand the virtual or non virtual thing? Are you speaking of the OOP term virtual like virtual methods or classes in C++? I don't remember much about it, it's been nearly 10 years since I first learned the fundamentals of C++ and OOP, and been at least 4 or 5 since I used it on a regularly basis.... How does it relate to optimization.. maybe this goes out of the q.'s scope.... –  Chris Okyen Nov 13 '12 at 20:12
    
@ChrisOkyen: In Java all methods are virtual (called based on runtime type), but when the compiler can prove the method won't be overridden (the class or method are final), it calls the implementation directly without consulting the instance metadata, which is faster. C++ const does not interfere with this, but creating a read-only interface does, because interface methods can't be final. –  Jan Hudec Nov 14 '12 at 9:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.