Classes are the core concept of OOP (Object-oriented programing). Java offers the concept constructor that is directly associated with the classes as constructors hold exactly the same name as the class name. In java, the prime objective of constructor is to initialize the class objects. A java class can have several constructor, but their definition mus be different in terms of parameters.


Java Constructor යනු කුමක්ද?

Java Constructor යනු Class එකේ නමට සමාන නමක් ඇති නමුත් කිසිදු Return type එකක් නොමැති විශේෂ ක්‍රමයකි. යමෙකු Class එකක Object එකක් සාදන විට, එම Class එකේ නමින්ම Constructor auto ම සෑදේ. Object එක Run කිරීමට Java Constructor භාවිතා කරයි.

Type of Java Constructor

Default Constructor.

Parameterized Constructor.

 

Default Constructor.

Program එකක් Run කිරීමේ දී, Java Compiler විසින් Automatically Constructor (Without argument) සාදයි. මෙය Default Constructor ලෙස හදුන්වයි.

  • Example

 

Parameterized Constructor.

The constructor that can accept multiple values/parameters as argument are referred to as the parameterized constructor.

  • Example

Copyright © 2022 All Right Reseved