Immutable Method in Java

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?

1

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.

5

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.

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

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

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật