

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The Initializer Block in Java
The Initializer block is used to declare constructors’ common parts. Let us see an example −
Example
import java.io.*; public class Demo{ { System.out.println("The common constructor has been invoked"); } public Demo(){ System.out.println("The default constructor has been invoked"); } public Demo(int x){ System.out.println("The parametrized constructor has been invoked"); } public static void main(String arr[]){ Demo my_obj_1, my_obj_2; System.out.println("The Demo objects have been created."); my_obj_1 = new Demo(); my_obj_2 = new Demo(89); } }
Output
The Demo objects have been created. The common constructor has been invoked The default constructor has been invoked The common constructor has been invoked The parametrized constructor has been invoked
A class named Demo contains a constructor without a parameter, a parameterized constructor, and the main function. Inside the main function, an instance of the Demo class is created, one with parameter, and one without parameter.
- Related Questions & Answers
- instance initializer block in Java
- Why use instance initializer block in Java?
- Initializer for final static field in Java
- Object Initializer in C#
- Object initializer in JavaScript
- Java static block
- What is the try block in Java?
- What is the catch block in Java?
- What is the finally block in Java?
- Method and Block Synchronization in Java
- A static initialization block in Java
- C++17 If statement with initializer
- Can we have a try block without a catch block in Java?
- A non-static initialization block in Java
- What are block lambda expressions in Java?
Advertisements