Java – Declaration, Definition, Initialization and Instantiation, Instance of class

In this article, we will discuss some of the key terms used in programming languages.

Often, we get confused with few terms in the programming world;

  • Declaration
  • Defining or definition
  • Initialization
  • Instantiation
  • Instance of a class

Java Class

  • First, let us try to understand what all a class contains in OOPS or in specific Java;
  • A class consists of
    • member variables (fields)
    • behavior (methods or functions)

Student.java

package in.bench.resources.key.terms.in.java;

public class Student {

	// member variables or fields
	int studentRollNo;
	String studentName;
	int studentAge;

	// instance method of a class
	public void getStudentResult() {
		// method definition goes here
	}
}

Let us go through each terms in details with example & explanation;

1. Declaration:

  • So when a member variables is declared without any value associated with it OR
  • When method is just declared (method prototype with no body & ending with semi-colon(;))
  • Then, it is simply referred as declaration
  • Example: interface and abstract methods inside abstract class
  • Note: all methods inside interface are abstract till Java 7 version

Example 1: with interface

Student1.java

package in.bench.resources.key.terms.in.java;

public interface Student1 {

	// member variables or fields

	// methods
	public void getStudentDetails();
}

Example 2: with abstract class

Student2.java

package in.bench.resources.key.terms.in.java;

public abstract class Student2 {

	// member variables or fields
	int studentRollNo;
	String studentName;
	int studentAge;

	// abstract method
	public abstract void getStudentDetails();
}

2. Definition or Initialization:

  • Now, when we declare any member variable & assign some values to variables, then member variable is said to defined or initialized
  • Also, if a method is defined with body (with logic inside opening-closing curly braces), then it is said to be method definition (as against abstract method when we said just declaration)
  • Examples: concrete classes

Student.java

package in.bench.resources.key.terms.in.java;

public class Student {

	// member variables or fields
	int studentRollNo = 1001;
	String studentName = "Pup";
	int studentAge = 35;

	// instance method of a class
	public void getStudentResult() {
		// method definition goes here
	}
}

3. Instantiation:

  • When a blue-print or full-fledged class is defined with its member variables and method definition, then we need to create or instantiate an object; so as to access all possible variables and methods
  • Instantiate is the very correct word used (instead of create, which is used to understand in laymen terms)

Student.java

package in.bench.resources.key.terms.in.java;

public class Student {

	// member variables or fields
	int studentRollNo = 1001;
	String studentName = "Pup";
	int studentAge = 35;

	// instance method of a class
	public void getStudentResult() {

		// method definition goes here

		System.out.println("Sample method "
				+ "invocation for Student with details: \n");
		System.out.println("Roll No.:- " + studentRollNo);
		System.out.println("Name:- " + studentName);
		System.out.println("Age:- " + studentAge);
	}

	// main() method
	public static void main(String args[]) {

		// instantiate or create an Object of type Student
		Student student = new Student();

		// invoking method using newly instantiated Student Object
		student.getStudentResult();
	}
}

Output:

Sample method invocation for Student with details: 

Roll No.:- 1001
Name:- Pup
Age:- 35

4. Instance of a class:

  • Whenever we instantiate or create an object of a class, then that particular reference variable is referred as instance of that class
  • Generally, when we instantiate an object, there is always new operator associted with it for the creation of new object
  • Using instanceOf operator, we can always check or verify whether particular reference variable is an instance of a class or NOT

Student.java

package in.bench.resources.key.terms.in.java;

public class Student {

	// member variables or fields
	int studentRollNo = 1001;
	String studentName = "Pup";
	int studentAge = 35;

	// instance method of a class
	public void getStudentResult() {

		// method definition goes here
	}

	// main() method
	public static void main(String args[]) {

		// instantiate or create an Object of type Student
		Student student = new Student();

		// verifying using instanceof operator
		if(student instanceof Student) {
			System.out.println("It is an instance "
					+ "of class Student !!");
		}
		else {
			System.out.println("NOT an instance "
					+ "of class Student !!");
		}
	}
}

Output:

It is an instance of class Student !!

Its time for you folks to reply back with your comments and sugestion

Happy Coding !!
Happy Learning !!

Java - Why the execution order of Constructor and Initialization blocks is pre-defined ?
Java - How to construct a singleton class in a multi-threaded environment ?