Reputation: 33966
I'm trying to set an Array only if it doesn't exist but I cannot figure out how to do it since I get an error saying it doesn't exist:
if (null == arr) { // arr cannot be resolved to a variable
arr = new ArrayList<task>();
}
Isn't there something such as isset()
from php
? Also what does <>
hold?
Upvotes: 1
Views: 3477
Reputation:
In this case, since you're initializing arr
as an ArrayList
type, I will assume you have already declared the type of arr
as ArrayList<task>
or List<Task>
.
If arr
has not been initialized, it will produce an error if you attempt to compare it. Thus, you must initialize arr
to null:
List<task> arr = null;
This way, when you run your code, you can simply use the expression:
if(arr == null)
arr = new ArrayList<task>();
Upvotes: 1
Reputation: 483
List<Task> arr=null;
if (null == arr) { // arr cannot be resolved to a variable
arr = new ArrayList<task>();
}
it wiill work fine
Upvotes: 0
Reputation: 34900
No, you can not. Java
is the language with strong static typing. There isn't any isset
like in JS
.
The only way is to declare
your variable arr
before and assign null
value to it.
Upvotes: 1