-1

Is it possible to create an int variable with no value, then quantify it in my code, and then "unquantify" it so that it no longer has a value?

Example:

class Test {

    int integer;

    public static void main (String[] args) {

        integer = 1;
        ...
        //do stuff
        ...
        integer = null; //Does this line set integer's value to nothing?
                        // I want to be able to reset the value of integer back to what it was when I defined it (the value was nothing, it had no value)

    }
}

EDIT: grammar

Nick
  • 61
  • 8
  • 5
    A variable can't go back to being "undeclared" unless you're changing scopes. Ex. Declare a variable local to a method, then declare a variable with the same name local to another method. But even in this case those two aren't the same variable in the end, they just share a name. Also, you can't set primitive types to null (such as int), you can try using the Integer class for that instead. What are you trying to accomplish with this anyway? – Zarwan Aug 30 '15 at 21:20
  • 3
    Why would you want to do this? Instead, ask about what you're *actually trying to do*. See [**What is the XY Problem?**](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – durron597 Aug 30 '15 at 21:23
  • Basically, all `int` variables _will_ have a value; there's no such thing as an `int` that has no value. If the variable is an instance variable, the value will be 0 if you don't initialize it. If it's a local variable, you won't know what the value is, but it doesn't matter because Java won't let you look at it before you assign it to something anyway. (Other languages like C will let you look at variables like that, and the values will be random garbage.) – ajb Aug 30 '15 at 21:24
  • 1
    Possible duplicate: http://stackoverflow.com/questions/11047276/null-for-primitive-data-types – Sandeep Chatterjee Aug 30 '15 at 21:24
  • 2
    As others have suggested, you can use `Integer` for a variable that could have no value (i.e. you can assign it to null). But in my opinion that is usually not the right approach, and wanting to do this often indicates there is a larger problem with your design. – ajb Aug 30 '15 at 21:26
  • Have a read of the `Default Values` section here http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html. Is setting the value to `0` enough to achieve what you want to do? – jwpfox Aug 30 '15 at 21:57

2 Answers2

1

You cannot assign null to a primitive type.

You could use the Integer object like this:

Integer i = null;

If you have an int as classmember and you access it inside a method the value of the int will be 0.

Also you have to make your variable static to access it in your main.

user
  • 735
  • 1
  • 6
  • 21
1

As an alternative to null you might like to look at the Optional class that was added in Java 8. It is useful for declaring variables that may or may not have a value:

Optional<Integer> value = Optional.empty();
value = Optional.of(4);
value.ifPresent(n -> method(n));

If used well it can make the code handling optional values much more obvious and transparent than using null.

sprinter
  • 27,148
  • 6
  • 47
  • 78