
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to create a default constructor in Java?
Default constructor (No-arg constructor)
A no-arg constructor doesn’t accepts any parameters, it instantiates the class variables with their respective default values (i.e. null for objects, 0.0 for float and double, false for Boolean, 0 for byte, short, int and, long).
There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.
Rules to be remembered
While defining the constructors you should keep the following points in mind.
A constructor does not have return type.
The name of the constructor is same as the name of the class.
A constructor cannot be abstract, final, static and Synchronized.
You can use the access specifiers public, protected & private with constructors.
Example
class NumberValue { private int num; public void display() { System.out.println("The number is: " + num); } } public class Demo { public static void main(String[] args) { NumberValue obj = new NumberValue(); obj.display(); } }
Output
The number is: 0
Example
public class Student { public final String name; public final int age; public Student(){ this.name = "Raju"; this.age = 20; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { new Student().display(); } }
Output
Name of the Student: Raju Age of the Student: 20
- Related Articles
- Default constructor in Java
- Java default constructor
- How to create a parameterized constructor in Java?
- Java Program to Show Inherited Constructor Calls Parent Constructor By Default
- What is the purpose of a default constructor in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- How to create a constructor reference for an array in Java?
- Can we initialize static variables in a default constructor in Java?
- Do static variables get initialized in a default constructor in java?
- Default constructor in C#
- What do you mean by default constructor in Java?
- How to create a Default Cell Editor with textbox in Java?
- C++ default constructor
- What are the rules to create a constructor in java?
- What is a default constructor in JavaScript?

Advertisements