Java Constructor Examples
- Constructors are required to create objects for a class. Constructors are used to initialize the instance variables of an object.
- Constructor declaration looks like method declaration. It must have the same name as that of the class and have no return type.
- Constructors can be classified into two types, default constructors and parametarized constructors.
- If you don't define a constructor, then the compiler creates a default constructor. Default constructors do not contain any parameters. Default constructors are created only if there are no constructors defined by us.
- Prametarized constructors are required to pass parameters on creation of objects. We can overload constructors with different datatypes as its parameters.
- Use 'this()' to communicate from one constructor to another constructor in the same class.
- Use 'super()' to communicate with super class constructor.
Java Constructor Sample Code Examples
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Education is what remains after one has forgotten what one has learned in school.
-- Albert Einstein