Core Java

Instantiate Java Example

In this post, we feature a comprehensive article implementing an Instantiate Java example. The phrase “instantiating a class” means to create an object. An object instantiation java class provides the blueprint for objects, and we create an object from a class. For example, the statement – Animal doggy = new Animal(); has three parts to it.

  1. Declaration: Animal doggy declares a variable doggy and associates it with object type Animal.
  2. Instantiation: The new keyword is a Java operator that creates the object.
  3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

In this article, we will look into details about instantiation in Java.

1. Instantiation of primitives

The Java programming language is statically-typed, which means all variables must first be declared before they can be used. This involves stating the variable’s type and name. Example: int iCount=10;

A variable’s data-type determines the values it may contain and the operations that may be performed on it. A primitive type is predefined by Java and is named by a reserved word. The primitive types available in Java are – byte, short, int, long, float, double, boolean and char. It is not always necessary to assign a value when the field is declared. Fields (declared globally) that are declared but not initialized will be set to a reasonable default by the compiler. Generally, this default will be zero or null depending on the data type.

The new keyword is not used when initializing a variable of primitive type. Primitives are special data types built into the language and they are not objects created from a class. These literals are source code representation of a fixed value.

2. Instantiation of object

As we mentioned above, instantiation is the usage of the new keyword to create an object. This allocates memory for the new object and returns a reference to that memory. The new operator also invokes the object constructor. The new operator requires a single, postfix constructor call. The name of the constructor provides the name of the class to instantiate. A class can have more than one constructor, each having different signatures. The Java compiler differentiates the constructors based on the number and type of arguments. If a class does not explicitly declare any constructors, the Java compiler automatically provides a no-argument constructor, called the default constructor. Let us look at an example that defines the different constructors and their initialization.

Rectangle.java

public class Rectangle{
	public int width = 10;
	public int height = 10;
	
	public Rectangle(){
		System.out.println("default with height and width as 10");
	}
	public Rectangle(int w){
		System.out.println("constructor with width as argument");
		this.width=w;
	}
	public Rectangle(int w, int h){
		System.out.println("constructor with height and width as arguments");
		this.width=w;
		this.height=h;
	}
	public void dimensions(){
		System.out.println("Width :" + width + " Height :"+height);
	}
}

InstantiationExample.java

public class InstantiationExample{
	public static void main (String args[]){
		Rectangle rect1 = new Rectangle();
		rect1.dimensions();
		Rectangle rect2 = new Rectangle(20);
		rect2.dimensions();
		Rectangle rect3 = new Rectangle(20,20);
		rect3.dimensions();
	}
}

The output of the above code would be:

default with height and width as 10
Width :10 Height :10
constructor with width as argument
Width :20 Height :10
constructor with height and width as arguments
Width :20 Height :20

3. Download the source code

Download
You can download the full source code of this example here: Instantiate Java Example

Venkat-Raman Nagarajan

Venkat works for a major IT firm in India and has more than a decade of experience working and managing Java projects for a banking client.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button