Posts

Java OOPS:Constructors in Java – A complete study!!

Image
  Constructors in Java – A complete study!! Constructor is a block of code that initializes the newly created object. A constructor resembles an instance method in java but it’s not a method as it doesn’t have a return type. In short constructor and method are different(More on this at the end of this guide). People often refer constructor as special type of method in Java. Constructor has same name as the class and looks like this in a java code. public class MyClass { //This is the constructor MyClass (){ } .. } Note that the constructor name matches with the class name and it doesn’t have a return type. How does a constructor work To understand the working of constructor, lets take an example. lets say we have a class  MyClass . When we create the object of  MyClass  like this: MyClass obj = new MyClass () The  new keyword  here creates the object of class  MyClass  and invokes the constructor to initialize this newly created object. You may get a little lost h