JAVA CONSTRUCTORS
DEFINITION
.
SYNTAX
ADVANTAGES
CONTENTS
TYPES OF
CONSTRUCTORS
EXAMPLE CODES
DEFINITION
A constructor is a special method within a
class that facilitates the initialization of
objects. Its primary responsibility is to set up
the initial state of an object when it is created.
Constructors have the same name as the class
they belong to and lack a return type,
distinguishing them from regular methods.
TYPES OF CONSTRUCTORS
1.Default Constructor
2.Parameterized Constructor
DEFAULT
CONSTRUCTOR
A Default constructor is a
special constructor
automatically generated by the
compiler if no constructor is
explicitly defined within a class.
This constructor is provided by
default and is responsible for
initializing the object's fields to
their default values.
SYNTAX
public class MyClass {
// Default Constructor
public MyClass() {
// Initialization code
}
}
OUTPUT
class Test
{
int i;
String s;
Test()
{
System.out.println("No-arg Constructor");
}
public static void main(String[] args)
{
Test t = new Test();
System.out.println("i : "+t.i);
System.out.println("s : "+t.s);
}
}
EXAMPLE CODE
PARAMETERIZED
CONSTRUCTOR
A Parameterized constructor is a type of
constructor that accepts parameters
during its invocation, allowing for the
initialization of object properties with
specific values , enabling more dynamic
and customized object creation
SYNTAX
public class MyClass {
// Parameterized Constructor
public MyClass(int param1, String param2)
{
// Initialization code using parameters
}
}
OUTPUT
EXAMPLE CODE
class Test2
{
int id;
String name;
Test2(String name , int id)
{
this.name=name;
this.id=id;
}
public static void main(String[] args)
{
Test2 s1 = new Test2("Felix" , 1409);
Test2 s2 = new Test2("Theo" , 7100);
System.out.println("NAME : "+s1.name+ " , "+"ID :
"+s1.id);
System.out.println("NAME : "+s2.name+ " , "+"ID :
"+s2.id);
}
}
ADVANTAGES
Using constructors in your Java programs provides
several advantages that contribute to better code
design, maintainability, and reliability. Key
advanatges include :
Object Initialization
Code Readability
Encapsulation
Support for Inheritance
Immutable Objects etc...
THANK YOU

Java constructors and types with examples

  • 1.
  • 2.
  • 3.
    DEFINITION A constructor isa special method within a class that facilitates the initialization of objects. Its primary responsibility is to set up the initial state of an object when it is created. Constructors have the same name as the class they belong to and lack a return type, distinguishing them from regular methods.
  • 4.
    TYPES OF CONSTRUCTORS 1.DefaultConstructor 2.Parameterized Constructor
  • 5.
    DEFAULT CONSTRUCTOR A Default constructoris a special constructor automatically generated by the compiler if no constructor is explicitly defined within a class. This constructor is provided by default and is responsible for initializing the object's fields to their default values. SYNTAX public class MyClass { // Default Constructor public MyClass() { // Initialization code } }
  • 6.
    OUTPUT class Test { int i; Strings; Test() { System.out.println("No-arg Constructor"); } public static void main(String[] args) { Test t = new Test(); System.out.println("i : "+t.i); System.out.println("s : "+t.s); } } EXAMPLE CODE
  • 7.
    PARAMETERIZED CONSTRUCTOR A Parameterized constructoris a type of constructor that accepts parameters during its invocation, allowing for the initialization of object properties with specific values , enabling more dynamic and customized object creation SYNTAX public class MyClass { // Parameterized Constructor public MyClass(int param1, String param2) { // Initialization code using parameters } }
  • 8.
    OUTPUT EXAMPLE CODE class Test2 { intid; String name; Test2(String name , int id) { this.name=name; this.id=id; } public static void main(String[] args) { Test2 s1 = new Test2("Felix" , 1409); Test2 s2 = new Test2("Theo" , 7100); System.out.println("NAME : "+s1.name+ " , "+"ID : "+s1.id); System.out.println("NAME : "+s2.name+ " , "+"ID : "+s2.id); } }
  • 9.
    ADVANTAGES Using constructors inyour Java programs provides several advantages that contribute to better code design, maintainability, and reliability. Key advanatges include : Object Initialization Code Readability Encapsulation Support for Inheritance Immutable Objects etc...
  • 10.