Java Object initialization using Constructor | Java Online Course

This Java Online Course “Java Object initialization using Constructor” post explains clearly how to initialize object using parameter constructor in the process of object creation process.


 

Initialization of Object:

  • Non static variables get memory allocation inside Object.
  • These variables are called instance variables.
  • It is not recommended to initialize instance variables directly.
  • We must initialize these variables using constructor.
  • All the non-static variables initializes with default value in object creation process.

 

  • We need to initialize non static variables using constructor.
  • Arguments of any method working like “local variables”.
  • We must access non static members from non static context using “this”.

 

  • In the following program ‘x’ value is directly assigned to non static variable ‘a’ because local variable with the name ‘a’ is not present.

 

  • If local variable is present with the name, value re-assigned back to local variable only.
  • In this case, non static variable holds only default value.

 

Share this