Programmingempire
This article demonstrates Parameterized Constructor Example in Java. At first, we describe constructors in a class.
Role of Constructor in a Class
Since a class contains insance variables that each object of the class has its own copy, so it also needs a mechanism to initialize those instance variable. Indeed, a constructor provides such mechanism to a class. In general, constructors are special functions which are defined within a class to initialize its instance variables. Like functions, the constructors can also take parameters. However, there is no return type for a constructors. Accordingly, constructors don’t return any value.
Types of Constructors
Constructors are classified as parameter-less constructors, and parameterized constructors. Further, we call the parameterless constructor as default constructor. While, we can have any number of parameterized constructors, we can have only one default constructor. Moreover, if we don’t define a default constructor, the compiler itself provides one.
Defining the Default Constructor
The following syntax and example shows how to define the default constructor.
Syntax:
<access-modifier> class-name(){
//initialization statements
}
Example:
class MyClass{
int a;
public MyClass(){
a=1;
}
}
Defining Parameterized Constructor
Likewise, we can define the parameterized constructor as follows.
Syntax:
<access-modifier> class-name(parameter-list){
//initialization statements
}
Example:
class MyClass{
int a;
public MyClass(int x){
a=x;
}
}
The Following Section shows Parameterized Constructor Example in Java
While, the subsequent examples demonstrate the use of parameterized constructor, first look at the default constructor. The following code showsl the usage of default constructor. As can beseen, the default constructor assigns a constant value to the instance variable. Besides, each instannce of the class ConstructorDemo will get the same initial value for the instance variable mytext.
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
ConstructorsDemo ob=new ConstructorsDemo();
}
}
class ConstructorsDemo
{
String mytext;
public ConstructorsDemo()
{
mytext="Sample text assigned in default constructor";
System.out.println("It is a Default Constructor!");
System.out.println("mytext = "+mytext);
}
}
Output

Creating a Parameterized Constructor
Likewise, we can also create a parameterized constructor. The following code sows that the ConstructorDemo class has a parameterized constructor. While creating objects, we can pass the parameter that initializes the instance variable of the object.
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
ConstructorsDemo ob=new ConstructorsDemo("Parameterized Constructor");
}
}
class ConstructorsDemo
{
String mytext;
public ConstructorsDemo(String p)
{
mytext=p;
System.out.println("It is a Parameterized Constructor!");
System.out.println("mytext = "+mytext);
}
}
Output

Using this Keyword in Constructor
Basically, this keyword refers to the current object. Therefore, if both the instance variable and parameter name are same, then this keyword need to be used. As shown below, this keyword is used with instance variable.
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
ConstructorsDemo ob=new ConstructorsDemo("Using this keyword");
}
}
class ConstructorsDemo
{
String mytext;
public ConstructorsDemo(String mytext)
{
this.mytext=mytext;
System.out.println("It is a Parameterized Constructor!");
System.out.println("mytext = "+mytext);
}
}
Output

Using Object as Parameter
Also, we can use the object of the same class as parameter. As can be seen in the following example, we have two parameterized constructor. While the first one takes a string type parameter. The second one takes an object parameter. Accordingly, we can create an object by using other objects created earlier.
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
MyBlog ob1=new MyBlog("My Programming Blog");
MyBlog ob2=new MyBlog(ob1);
}
}
class MyBlog
{
String blogTitle;
public MyBlog(MyBlog ob)
{
blogTitle=ob.blogTitle;
System.out.println("It is a Parameterized Constructor using the object as parameter!");
System.out.println("Blog Title = "+blogTitle);
}
public MyBlog(String blogTitle)
{
this.blogTitle=blogTitle;
System.out.println("It is a Parameterized Constructor!");
System.out.println("Blog Title = "+blogTitle);
}
}
Output

- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
