Reputation: 11649
I know in Java, as a strongly typed language, every variable should be declared.
On the other hand Java is an object-oriented language and from every variable must make an instance. (To allocate the memory for the variable)
Here I have a code that we did not declare ArrayAdapter
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, presidents));
}
My question is: does creating an instance do exactly what declaring a variable does here?
Upvotes: 1
Views: 92
Reputation: 16209
This line:
setListAdapter(new ArrayAdapter<String>(...));
creates an instance of ArrayAdapter and passes a reference to that instance to the method setListAdapter`. There is no separate declaration step in this case (it's an anonymous reference).
You could also declare, initialize and pass the reference in separate steps:
// declare variable
ArrayAdapter adapter;
// initialize (create an instance and assign it to the declared variable)
adapter = new ArrayAdapter<String>(...);
// pass reference
setListAdapter(adapter);
So I hope from this example you see that declaring a variable is different from creating an instance.
Upvotes: 0
Reputation: 79848
This is a good question, because it points out one of the most significant differences between Java and C++, and one of the biggest hurdles for programmers making the transition from one of those languages to the other.
In C++, you can create an object simply by declaring a variable. That is, if you declare a variable of an object type (and not a reference variable), then when execution reaches the variable declaration, an object is created; and when the variable goes out of scope, the object is destroyed.
Java doesn't work this way - variables exist independently from the objects that they reference. If you declare a variable of object type and don't specify an object for it to refer to, then it will either be null initially (if it's a member of a class) or the compiler will force you to initialise it before you use it (if it's a local variable). But the point is, the variable can be in scope even when there's no object.
Also, an object can be created even when there's no variable to refer to it. Such objects are typically short lived, since if there's no reference anywhere to an object, the garbage collector will have its wicked way with the object.
If you come from a C++ background and you're making the transition to Java, it is worthwhile spending a lot of time understanding this distinction. A lot of elementary bugs come about because people don't understand it fully.
Upvotes: 1
Reputation: 69349
In Java, you only declare a variable if you need to store a reference to an object. This is optional, e.g.
new ArrayList<String>(); // just throws away the reference (pointless, really)
In your code example, there was no need to store a local reference to the ArrayAdapter
; the reference is only needed within the setListAdaptor()
method. So a local variable was not declared.
You could have written it:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, presidents);
setListAdapter(adapter);
}
But there is no real advantage in this case.
Upvotes: 1
Reputation: 10288
The part in your code like this:
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, presidents)
"declares" an instance of a variable, however your have not retained a reference to it. You could also do this:
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, presidents);
setListAdapter(myArrayAdapter);
and then it would be both declared and referenced for future use.
Upvotes: 0