I am wanting to create an object that can store an integer value and call various methods on it. The code is like this
public class IntegerUtil{
private int value;
public IntegerUtil(int a){
this.value = a;
}
//insert methods here
}
When I instantiate an IntegerUtil object, I do something like
IntegerUtil iu = new IntegerUtil(5); //this does compile
I would prefer to do this instead of typing out the entire constructor.
IntegerUtil iu = 5; //this does not compile
The compiler doesn't accept this as valid syntax because I am setting a non primitive data type equal to a literal. But it is possible to do this. I noticed that the java.lang.Integer class can be set equal to a literal.
int integer = 5; //primitive data type set equal to literal
Integer integer = 5; //object set equal to literal - how to do this?
Integer integer = new Integer(5); //object being instantiated with constructor
//all three of these compile with no problem
Is there a way I can make my IntegerUtil object behave like the java.lang.Integer class in that it can be initialized as a literal?