Object Referencing
A variable can hold either a primitive value or a reference to an object (it
doesn't actually hold the object itself).
Can think object references like C pointers, a reference "points to" an object.
Declaring a reference initially points it to a “null”.
Can also set an object to point to nothing using the keyword "null".
Once an object reference is declared it can then be assigned to “point to” an object by invoking the class constructor using the keyword “new”.
This will instantiates the object:
Null Pointer Exceptions
Note: Trying to use a reference without instantiating it first will result in a NullPointerException been thrown causing the program to terminate.
Tips: because a reference just points an object, it can be reassigned to point to a new Object
Wrapper classes
Null
Instances(实例)
Each time that we construct a new object we get back an object instance (or just instance).
Each instance is stored in a different, separate part of memory.
Multi variable scan store the same object reference:
Constructor
It is an easier way to assign initial values to an object’s attributes.And it returns an object reference.
Define a class with a constructor as the method:
“this” is a special Java keyword used in constructors to represent the object we are constructing
Missing Informationwhen Constructing Objects
1.不允许用户将某一信息为空(为空则弹出提示且不允许下一步操作)。
2.提前将该变量设置好默认值。
Using Scanner to Get The Values to Construct a Person
注意按照constructor的写法 将constructor的部分和其他部分分开写!
Default Constructor
A constructor that takes no parameters (zero-argumentconstructor or the zero-parameter constructor)
It’s always a good idea to have a default constructor in your class.
The Structure of A Class
再JAVA里 如果不写无参数constructor 编译器会默认帮补上,所以上图可以不写无参数的constructor