简体   繁体   中英

What is difference between new keyword for create object and declaring class variable?

enter image description here

what is difference between declaring varaible object and object vreated using new keyword?

  1. public Employee employee; --> This mean that a reference variable named employee is created but it does not point to anything. The value of employee variable is null. You can not use it directly as it does not have reference to any memory location.
  2. public Employee employee = new Employee(); --> This implies that, a call is given to a constructor function of class will be allocate a reference to the object in memory. You can use this object to access properties and methods of that class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM