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
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Constructors in JAva.pptx
1.
2. Objectives :
• What is constructor in java?
• Types of constructor in java
• Default constructor
• No-args constructor
• Parameterized constructor
3. 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
4.
5. Default constructor
• A default constructor is a constructor that
either has no parameters
• It has parameters, all the parameters have
default values.
7. 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);
}
}
9. 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.
11. 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);
}
}
13. 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.
15. 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");
}