Can we use constructors with parameters what kind of parameters can be given?
Unlike Java methods, a constructor has the same name as that of the class and does not have any return type. For example, Show
class Test { Test() { // constructor body } } Here, 7 is a constructor. It has the same name as that of the class and doesn't have a return type.Recommended Reading: Why do constructors not return values Example 1: Java Constructor
Output: Constructor Called: The name is Programiz In the above example, we have created a constructor named 8. Inside the constructor, we are initializing the value of the name variable.Notice the statement of creating an object of the Main class.
Here, when the object is created, the 8 constructor is called. And, the value of the name variable is initialized.Hence, the program prints the value of the name variables as Constructor Called: The name is Programiz0. Types of ConstructorIn Java, constructors can be divided into 3 types:
1. Java No-Arg ConstructorsSimilar to methods, 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. For example,
Example 2: Java private no-arg constructor
Output: Constructor is called Value of i: 5 In the above example, we have created a constructor 8. Here, the constructor does not accept any parameters. Hence, it is known as a no-arg constructor.Notice that we have declared the constructor as private. Once a constructor is declared Constructor Called: The name is Programiz2, it cannot be accessed from outside the class. So, creating objects from outside the class is prohibited using the private constructor. Here, we are creating the object inside the same class. Hence, the program is able to access the constructor. To learn more, visit Java Implement Private Constructor. However, if we want to create objects outside the class, then we need to declare the constructor as Constructor Called: The name is Programiz3. Example 3: Java public no-arg constructors
Output: Company name = Programiz Recommended Reading: Java Access Modifier 2. Java Parameterized ConstructorA Java constructor can also accept one or more parameters. Such constructors are known as parameterized constructors (constructor with parameters). Example 4: Parameterized constructor
Output: 0In the above example, we have created a constructor named 8. Here, the constructor takes a single parameter. Notice the expression, 1Here, we are passing the single value to the constructor. Based on the argument passed, the language variable is initialized inside the constructor. 3. Java Default ConstructorIf we do not create any constructor, the Java compiler automatically create a no-arg constructor during the execution of the program. This constructor is called default constructor. Example 5: Default Constructor 2Output: 3Here, we haven't created any constructors. Hence, the Java compiler automatically creates the default constructor. The default constructor initializes any uninitialized instance variables with default values. TypeDefault Value Constructor Called: The name is Programiz5false Constructor Called: The name is Programiz60 Constructor Called: The name is Programiz70 Constructor Called: The name is Programiz80 Constructor Called: The name is Programiz90L 0\u0000 10.0f 20.0d 3Reference nullIn the above program, the variables a and b are initialized with default value 0 and 4 respectively.The above program is equivalent to: 4The output of the program is the same as Example 5. Important Notes on Java Constructors
Constructors Overloading in JavaSimilar to Java method overloading, we can also create two or more constructors with different parameters. This is called constructors overloading. Example 6: Java Constructor Overloading 5Output: 6In the above example, we have two constructors: 8 and 1. Here, both the constructor initialize the value of the variable language with different values.Based on the parameter passed during object creation, different constructors are called and different values are assigned. It is also possible to call one constructor from another constructor. To learn more, visit Java Call One Constructor from Another. Note: We have used 2 keyword to specify the variable of the class. To know more about 2 keyword, visit Java this keyword.
Can we use constructors with parameters what type of parameters can be passed for this?You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.
Can we use constructor with parameters?Constructors can also take parameters, which is used to initialize attributes.
What is a constructor with parameters called?Parameterized Constructor – A constructor is called Parameterized Constructor when it accepts a specific number of parameters. To initialize data members of a class with distinct values.
Can we use constructor with parameters in Java?A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor. Example: Java.
|