N
Nan Li
Hello,
In C++, when you say const A* p, it means the object that p
points to is a constant, while A* const p means p is a constant. I
have noticed in Java, 'final A p' is equivalent to the second form in
C++, where p is not reassignable. Does any one know if there is a way
to achieve the first form in java ? Thanks a lot.
Sample programs:
//ConstantTest.java
class A
{
public int i;
}
public class ConstantTest {
public static void main( String[] args ) {
final A a = new A();
a.i = 5;
a = new A(); //not OK
}
}
--------------------------------
//ConstantTest.cpp
//there is mem leak in the code, demo only.
class A
{
public:
int i;
};
int main() {
A* const a = new A();
const A* ya = new A();
a->i = 5;
a = new A(); //not OK
ya->i = 5; //not OK
ya = new A();
return 0;
}
In C++, when you say const A* p, it means the object that p
points to is a constant, while A* const p means p is a constant. I
have noticed in Java, 'final A p' is equivalent to the second form in
C++, where p is not reassignable. Does any one know if there is a way
to achieve the first form in java ? Thanks a lot.
Sample programs:
//ConstantTest.java
class A
{
public int i;
}
public class ConstantTest {
public static void main( String[] args ) {
final A a = new A();
a.i = 5;
a = new A(); //not OK
}
}
--------------------------------
//ConstantTest.cpp
//there is mem leak in the code, demo only.
class A
{
public:
int i;
};
int main() {
A* const a = new A();
const A* ya = new A();
a->i = 5;
a = new A(); //not OK
ya->i = 5; //not OK
ya = new A();
return 0;
}