The terms instantiate, declare, and initialize are easy to stumble upon while programming. But what exactly do these terms mean?
Declaring a variable means the first “mention” of the variable, which tells the compiler “Hello, I am here and can be used”. In a statically typed language like Java, this also means that the declared type of the variable is determined. The value itself is not determined during the declaration.
String name; int nbr;
Declaration of the variable “name” of type String and the variable “nbr” of type int.
The term initialization usually means the first assignment of a value to a variable.
String name = "Thomas"; int nbr = 5;
The variables “name” and “nbr” were declared in the first example, but not yet initialized. Now the values of the variables were “initially assigned with values”, thus initialized.
Note: The fact that the variables have not yet been initialized is not entirely true (see here), but relying on automatic initialization is such a bad programming style 🙂
The term instantiation actually has nothing to do with assigning a value to a variable, even if a new object is sometimes instantiated when a variable is initialized.
The term simply means the creation of a new object, i.e. an instance, from a class.
String name = new String("Thomas");
In Java, the instantiation of an object is always accompanied by a call to the constructor.
MCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More Java MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”. 1. Which of the…Read More