Java : Object Initialization
How to interpret the statements below:
Notes bean = 开发者_开发百科null;
bean = new Notes();
Small explanation would be very helpful.
Are you sure the sequence of the instruction is not the opposite?
Notes bean = null;
bean = new Notes()
It seems a mistake :-)
Maybe could be:
Notes bean = new Notes();
bean = null;
In this case it can be used to signal to the garbage Collector that it has to trash the object referenced by Bean.
Otherwise I guess it a mistake. Could you post more code?
one step back:
Notes bean;
this is a field or a local variable declaration without explicit initialization.
A field will be initialized with a default initial value: null
for objects, 0
for primitive numbers and char, and false
for boolean.
A local variable will stay uninitialized. It's an error to access such variable until a value is assigned to it.
bean = new Notes();
is assigning a value to the field or variable. In this case a new instance of Notes
is created and assigned* to bean
. Similar to:
bean = null; // kind of assigning 'nothing' or 'empty'
bean = someMethod(); // assigning the value returned by the method
* actually it is the reference to the instance that is assigned to bean
Notes bean = null;
is the combination of both: declaring a field or variable, and assigning a value to it (initializing it). In this case the value is null
, which means the same as no instance.
These is almost equivalent to writing
Notes bean;
bean = null;
The variable could also be initialized with an new instance of Notes
:
Notes bean = new Notes();
new creates a new instance of the object, which allows you to use it as expected (such as accessing its methods)
null essentially means it references nothing (and thus you can't use any of its methods)
First statement creates an initialize fields on the bean object (a Notes object),
Notes bean = null
, is the declaration of a bean reference that doesn't reference to an actual object (that's the meaning of null), referencing to null means that you can't use bean and its methods.
If the statementes are within the same block of code (enclosed by the same pair of {}
), the program won't compile, because you're declaring the same reference twice.
bean = new Notes();
This assumes that you have a variable named "bean" that has already been declared. The type of bean must be Notes or one of its parent classes or an interface implemented by Notes. The "new" keyword along with the parameter-less constructor call signify the creation of a new object of type Notes and the variable bean references that new instance.
Notes bean = null;
This declares a variable named "bean" of type Notes. It also initializes the variable to null. Variables that are fields are initialized to null by default (unless they are primitives), but local variables are not initialized to anything by default. Thus this line explicitly initializes bean to null.
bean = new Notes();
This statement assumes you've already delcared a variable named bean of type Notes (or any of its parent classes. If you just run this line by itself, it will blow up because it can't find the variable 'bean'.
Notes bean = null;
This here will initialize a variable 'bean' and have it point to nothing. A variable is just a pointer to a spot in memory where your object will reside.
You should switch the order of these 2 statements, or combine then like:
Notes bean = new Notes();
精彩评论