Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Video lesson

Understanding Parameterized Constructors in Java

In this lesson, you will learn

  • Parameterized Constructor
  • Examples

 

Parameterized Constructor

  • A parameterized constructor in Java is a constructor that accepts one or more arguments (parameters).
  • These parameters allow you to initialize the object’s instance variables with specific values when the object is created. This provides flexibility and control over how objects are initialized.

 

Why Use Parameterized Constructors?

  • Initialize Objects with Specific Values: You can set the initial state of an object based on the values passed as arguments. This avoids having to use setter methods after object creation.
  • Data Validation: You can perform validation checks within the constructor to ensure that the values passed are valid before assigning them to the instance variables. This helps maintain data integrity.
  • Code Conciseness: Initializing objects directly during creation is often more concise and readable than using separate setter methods.

 

    Example 1: Parameterized Constructor

    package ch7.l7;
    
    class Rectangle {
        double length;
        double width;
     
        // Parameterized constructor
        public Rectangle(double l, double w) {
            length = l;
            width = w;
        }
        //Method to Calculate Area
        double calculateArea() {
            return length * width;
        }
    }
     
    public class RectangleMain {
        public static void main(String[] args) {
        	
            Rectangle rect1 = new Rectangle(5.0, 3.0);
            Rectangle rect2 = new Rectangle(5.0, 3.0);
            
            System.out.println("Area of rect1: " + rect1.calculateArea());
            System.out.println("Area of rect 2: " + rect2.calculateArea());
        }
    }
    

    Output

    Area of rect1: 15.0
    Area of rect 2: 450.0
    

     

    Example 2: Student Example with Data Validation

    class Student {
        private String name;
        private int age;
    
        public Student(String name, int age) {
            this.name = name;
            if (age >= 0 && age <= 150) { // Data validation
                this.age = age;
            } else {
                System.out.println("Invalid age for student " + name + ". Setting age to 0.");
                this.age = 0; // Or throw an exception
            }
        }
    
        // Getters
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    public class ParameterizedConstructorExample2 {
        public static void main(String[] args) {
            Student student1 = new Student("Alice", 20);
            System.out.println("Student 1: Name = " + student1.getName() + ", Age = " + student1.getAge());
    
            Student student2 = new Student("Bob", -5); // Invalid age
            System.out.println("Student 2: Name = " + student2.getName() + ", Age = " + student2.getAge()); // Age will be 0
    
            Student student3 = new Student("Charlie", 160); // Invalid age
            System.out.println("Student 3: Name = " + student3.getName() + ", Age = " + student3.getAge()); // Age will be 0
    
        }
    }
    
    
    

    Output

    Rectangle 1: Length = 10, Width = 5
    Area = 50
    Rectangle 2: Length = 7, Width = 12
    Area = 84
    

     

     


    End of the lesson….enjoy learning

     

     

    Student Ratings and Reviews

     

    5.0
    5.0 out of 5 stars (based on 2 reviews)
    Excellent100%
    Very good0%
    Average0%
    Poor0%
    Terrible0%

     

     

    05/04/2025

    Every topic is covered….Videos are very helpful

    02/04/2025

    In this chapter i was learn about prameterized constructor .

    Videos are very helpful to learn the concepts .

    and the best part is sir explain the concept with the help of example .

     

     

    Submit a Review