Witscale Test Center

1.5 Object creation with constructors > 1.5.1 The new keyword


1.5.1 The new keyword

An object is constructed by instantiating a class. The process of creating an object of a class is called as instantiation and the created object is called as an instance. To create a new object, Java uses the new keyword. Figure 1.5 illustrates how you can create an account object by instantiating the Account class.


 

Figure 1.5 Creating an account instance with new keyword.

 

The nekeyword followed by a call to the constructor method. In the example, Account(50) is a call to a constructor for the Account class. In this call, the account object is created. The new keyword returns a reference vcto this newly created Account object. You can then store this reference into the account variable.

 

Object’s methods are also called as instance methods and its variables as instance variables. Interestingly a class may define methods and variables, which belongs to a class rather than an object. Such methods and variables are called as static or class members. We will learn more about them in chapter 3.