Constructors in Java / Interview questions on constructors

 

Constructors in Java (Rules)

  • Ø  Name of the constructor should be same as class name.

    Ø  Constructor can have arguments.

    Ø  New operator is use to create memory at run time.

    Ø  Compiler placed a default constructor in class if user does not write any constructor in class.

    Ø  Constructor does not have any return type though it returns the instance of a class.

    Ø  Default constructor is provided by compiler.

    Ø  If user provide any constructor, then compiler will not generate default constructor.

    Ø  Constructor are auto executed when we want to execute a particular code whenever we create and object.

    Ø  Constructor are used to initialize instance variable.

    Ø  Constructor chaining will be achieve with the help of THIS keyword and THIS should be the first line In the constructor.



Post a Comment