OCA Java SE 7 Programmer I Certification Guide: Prepare for the 1ZO-803 exam

Author: Mala Gupta
4.5
This Month Stack Overflow 1

Comments

by anonymous   2017-08-20

Here is an explanation took from OCA Java SE 7 Programmer I Certification Guide: Prepare for the 1ZO-803 exam:

An object comes into the picture when you use the keyword operator new. You can initialize a reference variable with this object. Note the difference between declaring a variable and initializing it. The following is an example of a class Person and another class ObjectLifeCycle:

class Person {}
class ObjectLifeCycle {
    Person person;
}

In the previous code, no objects of class Person are created in the class ObjectLife-Cycle; it declares only a variable of type Person. An object is created when a reference variable is initialized:

class ObjectLifeCycle2 {
    Person person = new Person();
}

Syntactically, an object comes into being by using the new operator. Because Strings can also be initialized using the = operator, the following code is a valid example of String objects being created

class ObjectLifeCycle3 {
    String obj1 = new String("eJava");
    String obj2 = "Guru";
}