Constructor
It is a function that has same name as the class name. It can't have any return type , not even void. It is implicitly called whenever an object of a class is called. It is used to initialize the data members of the class
It is a function that has same name as the class name. It can't have any return type , not even void. It is implicitly called whenever an object of a class is called. It is used to initialize the data members of the class
There are two types of constructors in java.
- Default constructor:- it doesn't take any arguments.it initializes all the data members. If no Constructor is defined in the class then java compiler gives you a default constructor which assigns default values to the data members according their data type.(this constructor is taken back from the java compiler if the user defines even one constructor of its own)
- parameterized constructor:- it takes arguments and assigns these values to the data members. Example of Constructor
Class Student
{
int age, String name;
public Student() //default constructor
{
Age= 10;
Name= "abc"}
public Student (int a) //parameterized Constructor
{
Age=a;
Name="xyz";}
public Student (int a, String str) //parameterized Constructor
{
Age=a;
Name=str;
}
}Constructor overloading:
Example:
String str="Computer";
str.substrint(2); //output---> mputer (Extracts all the letters from index 2 to the last index.
str.substrint(2, 4); //output-->mp(Extracts letters from index 2 to 3)
Related Posts
Continue learning with these related articles

ISC Practical 2027: College Enrolment Code Validator and Stream Report – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 question: validate college enrolment codes made of a stream, year and serial number, give the exact reason for each rejection, auto-correct lowercase streams and prepare a stream-wise report.

ISC Practical 2027: Digital Lock Security Code Analyzer – Algorithm and Java Program
Fully solved ISC Computer Science Practical 2027 number-logic question: for a 5 to 10 digit security code, find the first and last digits, odd and even position sums, product of non-zero digits, digit frequency and classify the code as STRONG, MODERATE or WEAK.

String Methods in Java for ICSE Class 10: 25 Output Questions with Answers
ICSE Class 10 Computer Applications practice on String class methods: length, charAt, indexOf, lastIndexOf, substring, replace, trim, compareTo, equals and more, with 25 board-style output questions and step-by-step answers.