Objectives :
• What is constructor in java?
• Types of constructor in java
• Default constructor
• No-args constructor
• Parameterized constructor
What is constructor in java?
• Constructor are almost similar to methods except
for two things
• Name is same as the class name
• It has no return type
• Constructor in java is used to create the instance
of the class
Default constructor
• A default constructor is a constructor that
either has no parameters
• It has parameters, all the parameters have
default values.
Syntax:
Class classname
{
Test() {
// constructor body }
}
Example program:
class Main
{
private String name; // constructor Main()
{
System.out.println("Constructor Called:"); name = "Programiz";
}
static void main(String[] args) { // constructor is invoked while //
creating an object of the Main class Main obj = new Main();
System.out.println("The name is " + obj.name);
}
}
Output:
Constructor called: The
name is Programiz
No-args constructor
• A Java constructor may or may not have any
parameters (arguments).
• If a constructor does not accept any parameters
• It is known as a no-argument constructor.
Syntax:
Class classname
{
method name()// no arguments
{
------
}
}
Example program:
class Main
{
int i;
// constructor with no parameter private Main()
{
i = 5; System.out.println("Constructor is called");
}
public static void main(String[] args)
{
// calling the constructor without any parameter Main obj = new Main();
System.out.println("Value of i: " + obj.i);
}
}
Constructor is called Value of i: 5
Output:
Parameterized constructor
• Parameterized constructors in java are the constructors in a class
which have one or more than one arguments.
• Parameterized constructors are used to create user instances of
objects with user defined states.
• There can be more than one parameterized constructor in a class.
Syntax:
Class classname
{
method name(p1,p2)//two parameters passed
{
------
}
}
Example program
class Main
{
String languages; // constructor accepting single value Main(String lang)
{
languages = lang; System.out.println(languages + " Programming
Language"); } public static void main(String[] args)
{
// call constructor by passing a single value Main obj1 = new
Main("Java");
Main obj2 = new Main("Python");
Main obj3 = new Main("C");
}
Output
java Programming Language
Python Programming Language
C Programming Language
Thank you

Constructors in JAva.pptx

  • 2.
    Objectives : • Whatis constructor in java? • Types of constructor in java • Default constructor • No-args constructor • Parameterized constructor
  • 3.
    What is constructorin java? • Constructor are almost similar to methods except for two things • Name is same as the class name • It has no return type • Constructor in java is used to create the instance of the class
  • 5.
    Default constructor • Adefault constructor is a constructor that either has no parameters • It has parameters, all the parameters have default values.
  • 6.
  • 7.
    Example program: class Main { privateString name; // constructor Main() { System.out.println("Constructor Called:"); name = "Programiz"; } static void main(String[] args) { // constructor is invoked while // creating an object of the Main class Main obj = new Main(); System.out.println("The name is " + obj.name); } }
  • 8.
  • 9.
    No-args constructor • AJava constructor may or may not have any parameters (arguments). • If a constructor does not accept any parameters • It is known as a no-argument constructor.
  • 10.
  • 11.
    Example program: class Main { inti; // constructor with no parameter private Main() { i = 5; System.out.println("Constructor is called"); } public static void main(String[] args) { // calling the constructor without any parameter Main obj = new Main(); System.out.println("Value of i: " + obj.i); } }
  • 12.
    Constructor is calledValue of i: 5 Output:
  • 13.
    Parameterized constructor • Parameterizedconstructors in java are the constructors in a class which have one or more than one arguments. • Parameterized constructors are used to create user instances of objects with user defined states. • There can be more than one parameterized constructor in a class.
  • 14.
  • 15.
    Example program class Main { Stringlanguages; // constructor accepting single value Main(String lang) { languages = lang; System.out.println(languages + " Programming Language"); } public static void main(String[] args) { // call constructor by passing a single value Main obj1 = new Main("Java"); Main obj2 = new Main("Python"); Main obj3 = new Main("C"); }
  • 16.
    Output java Programming Language PythonProgramming Language C Programming Language
  • 17.