What is the difference between-
Employee e = new Employee();
and
Employee e;
If Employee is a class. Where would we use it in programs to satisfy our code?
Thanks.
What is the difference between-
Employee e = new Employee();
and
Employee e;
If Employee is a class. Where would we use it in programs to satisfy our code?
Thanks.
The first example is an initialization. You create a new object and assign it to the variable e
:
Employee e = new Employee();
The second example is a declaration. You just associate a variable name with an object type:
Employee e;
As it was mentioned by @JBNizet whether the e
is initialized or not depends on where you declare this variable.
If it is class member then it gets null
as a default value. If it is a local variable then it doesn't get any default value(undefined).
See more Creating Objects
And Java: define terms initialization, declaration and assignment