w3resource

Java Constructor - Exercises, Practice, Solution


Java Constructors Exercises [10 exercises with solution]


[An editor is available at the bottom of the page to write and execute the scripts.  Go to the editor]

1. Default Constructor:
Write a Java program to create a class called "Cat" with instance variables name and age. Implement a default constructor that initializes the name to "Unknown" and the age to 0. Print the values of the variables.

Click me to see the solution

2. Parameterized Constructor:
Write a Java program to create a class called Dog with instance variables name and color. Implement a parameterized constructor that takes name and color as parameters and initializes the instance variables. Print the values of the variables.

Click me to see the solution

3. Constructor Overloading
Write a Java program to create a class called "Book" with instance variables title, author, and price. Implement a default constructor and two parameterized constructors:

  • One constructor takes title and author as parameters.
  • The other constructor takes title, author, and price as parameters.
  • Print the values of the variables for each constructor.

Click me to see the solution

4. Chaining Constructors
Write a Java program to create a class called Student with instance variables studentId, studentName, and grade. Implement a default constructor and a parameterized constructor that takes all three instance variables. Use constructor chaining to initialize the variables. Print the values of the variables.

Click me to see the solution

5. Write a Java program to create a class called "Book" with attributes for title, author, and ISBN, and methods to add and remove books from a collection.

Click me to see the solution

6. Constructor with Validation
Write a Java program to create a class called Account with instance variables accountNumber and balance. Implement a parameterized constructor that initializes these variables with validation:

  • accountNumber should be non-null and non-empty.
  • balance should be non-negative.
  • Print an error message if the validation fails.

Click me to see the solution

7. Constructor with Default Values
Write a Java program to create a class called Car with instance variables make, model, and year. Implement a parameterized constructor that initializes these variables and assigns default values if not provided. Print the values of the variables.

Click me to see the solution

8. Overloading Constructors with Different Data Types
Write a Java program to create a class called Point with instance variables x and y. Implement overloaded constructors:

  • One constructor takes int parameters.
  • Another constructor takes double parameters.
  • Print the values of the variables for each constructor.

Click me to see the solution

9. Constructor with Array Initialization
Write a Java program to create a class called Classroom with instance variables className and students (an array of strings). Implement a parameterized constructor that initializes these variables. Print the values of the variables.

Click me to see the solution

10. Constructor for Singleton Pattern
Write a Java program to create a class called Singleton that ensures only one instance of the class can be created. Implement a private constructor and a public static method to get the single instance of the class. Print a message indicating the creation of the instance.

Click me to see the solution

More to Come !

* To run the code mouse over on Result panel and click on 'RERUN' button.*

Java Code Editor


Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.