How can I define a constant in my class that returns a object? What I'm looking for is something like BigInteger.ZERO
Asked
Active
Viewed 374 times
-3

b_pcakes
- 2,452
- 3
- 28
- 45
-
Declare a static variable of that object type in your class. – SacJn Oct 21 '15 at 17:28
-
2`public static final BigInteger ZERO = new BigInteger(new int[0], 0);` – Andy Thomas Oct 21 '15 at 17:29
-
To be accurate the "constant" member of your class will not "return" anything. Methods return things. Do you want a method that return an immutable variable ? – Manos Nikolaidis Oct 21 '15 at 17:32
-
Will this create a new instance of the object every time? otherwise, how can I make sure it is immutable? – b_pcakes Oct 21 '15 at 17:40
2 Answers
0
This is how BigInteger.ZERO
is defined :
public static final BigInteger ZERO = new BigInteger(new int[0], 0);

Manos Nikolaidis
- 21,608
- 12
- 74
- 82
0
public class SomeClass {
public static final type CONST_NAME = new SomeObject();
...
}
Then you can do SomeClass.CONST_NAME

leeor
- 17,041
- 6
- 34
- 60