Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Default constructor in java

Date- Sep 07,2023

3997

In Java, a default constructor is a constructor that is automatically provided by the Java compiler if no other constructors are explicitly defined in a class. The default constructor has no arguments and is responsible for initializing the object's fields with default values.

Here are the characteristics of a default constructor:

  1. 1. No Arguments: A default constructor takes no arguments.

  2. 2. No Explicit Implementation: If you don't define any constructors in your class, Java will automatically generate a default constructor for you.

  3. 3. Initialization: The default constructor initializes instance variables to their default values based on their data types (e.g., 0 for numeric types, null for reference types, and false for boolean).

Here's an example of a class with a default constructor:

In this example, the class MyClass has two instance variables, number and text, and it also has a default constructor. When you create an object of this class without providing any constructor arguments, the default constructor is automatically invoked, and the fields are initialized with their default values.


package Tutorial_01;

public class MyClass {

	 int number;
	    String text;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	    // Instance variables
	   
	    MyClass obj = new MyClass(); // Calls the default constructor
	    System.out.println(obj.number); // Outputs 0
		System.out.println(obj.text); // Outputs null
	    
	   
	}

}

It's important to note that once you define any constructor in a class (parameterized or no-arg constructor), the default constructor provided by Java is not automatically generated. Therefore, if you want to retain a no-arg constructor, you should explicitly define it yourself, like in the example above.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page