Constructors
While designing a class, the class designer can define within the class, a special
method called ‘constructor’
Constructor is automatically invoked whenever an object of the class is created
Rules to define a constructor
A constructor has the same name as the class name
A constructor should not have a return type
A constructor can be defined with any access specifier (private, public), constructor
declaration to control its access i.e which other class can call the constructor.
A constructor in Java can not be abstract, final, static and Synchronized.
A class can contain more than one constructor, So it can be overloaded
class Sample public class Main
{ {
private int id; public static void main(String[] args)
Sample() {
{ Sample obj1=new Sample();
id=101; Sample obj2=new Sample(102);
System.out.println(id); }
}
Sample(int no) }
{
id=no;
System.out.println(id);
}
}
Java Constructor Java Method
A constructor is used to initialize the state of A method is used to expose the behavior of
an object. an object.
A constructor must not have a return type. A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default The method is not provided by the compiler
constructor if you don't have any constructor in any case.
in a class.
The constructor name must be same as the The method name may or may not be same
class name. as class name.
No-argument constructor: A constructor that has no parameter is known as default constructor.
If we don’t define a constructor in a class, then compiler creates default constructor(with no
arguments) for the class. And if we write a constructor with arguments or no-argument then
compiler does not create default constructor.
Default constructor provides the default values to the object like 0, null etc. depending on the
type.
C:\Java\win22-23\C2.java
Parameterized Constructor: A constructor that has parameters is known as parameterized
constructor. If we want to initialize fields of the class with your own values, then use
parameterized constructor.
C:\Java\win22-23\C2c.java
// Java Program to illustrate calling of class GFG
// parameterized constructor. {
import java.io.*; public static void main (String[] args)
{
class Geek // this would invoke parameterized
{ constructor.
// data members of the class. Geek geek1 = new Geek("adam", 1);
String name; System.out.println("GeekName :" +
int id; geek1.name +
" and GeekId :" + geek1.id);
Geek(String name, int id) }
{ }
this.name = name;
this.id = id; Output:
}
} GeekName :adam and GeekId :1
import java.util.*;
class Student{
int s1,s2,s3;
double avg;
char grade;
String name;
Student()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Student name");
name=sc.nextLine();
System.out.println("Enter "+ name+" three subject marks");
s1=sc.nextInt();
s2=sc.nextInt();
s3=sc.nextInt();
}
void Avg()
{
avg=(s1+s2+s3)/3.0;
}
void Grade()
{
if(avg>90)
grade='S';
else if(avg>80)
grade='A';
else if(avg>70)
grade='B';
else if(avg>=60)
grade='C';
else
grade='N';
}
}
class StudentEx{
public static void main(String args[])
{
Student st1=new Student();
st1.Avg();
st1.Grade();
System.out.println(st1.name+" Grade is "+st1.grade);
}
import java.util.*;
class Student{
int s1,s2,s3;
double avg;
char grade;
String name;
Student(int a1,int a2,int a3,String sn)
{
s1=a1;
s2=a2;
s3=a3;
name=sn;
}
void Avg()
{
avg=(s1+s2+s3)/3.0;
}
void Grade()
{
if(avg>90)
grade='S';
else if(avg>80)
grade='A';
else if(avg>70)
grade='B';
else if(avg>=60)
grade='C';
else
grade='N';
}
}
class StudentEx2{
public static void main(String args[])
{
Student st1=new Student(91,80,56,"ABC");
st1.Avg();
st1.Grade();
System.out.println(st1.name+" Grade is "+st1.grade);
}