Course note:Object Referencing,Null Pointer Exception,Wrapper Classes,Instances,Constructor

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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值