Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 488989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:45:48+00:00 2026-05-13T01:45:48+00:00

I have a few cases I wonder about. First, if you have no constructor:

  • 0

I have a few cases I wonder about. First, if you have no constructor:

class NoCons { int x; }

When I do new NoCons(), the default constructor gets called. What does it do exactly? Does it set x to 0, or does that happen elsewhere?

What if I have this situation:

class NoCons2 extends NoCons { int y; }

What happens when I call new NoCons2()? Does NoCons‘s default constructor get called, and then NoCons2‘s constructor? Do they each set the respective x and y fields to 0?

What about this version:

class Cons2 extends NoCons { int y; public Cons2() {} }

Now I have a constructor, but it doesn’t call the super class’s constructor. How does x ever get initialized? What if I had this situation:

class Cons { int x; public Cons() {} }
class NoCons2 extends Cons { int y;  }

Will the Cons constructor be called?

I could just try all these examples, but I can’t tell when a default constructor is run. What’s a general way to think about this so that I’ll know what happens in future situations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:45:48+00:00Added an answer on May 13, 2026 at 1:45 am

    When a Java class has no constructor explicitly defined a public no-args default constructor is added so:

    class Cons { int x; } 
    

    is equivalent to:

    class Cons { int x; public Cons() {} }
    

    A subclass’s constructor that doesn’t explicitly define which of its parent’s constructor it calls will automatically call the default constructor in the parent class before it does anything else. So assuming:

    class A { public A() { System.out.println("A"); } }
    

    then this:

    class B extends A { public B() { System.out.println("B"); } }
    

    is exactly equivalent to:

    class B extends A { public B() { super(); System.out.println("B"); } }
    

    and the output in both cases will be:

    A
    B
    

    So when you do:

    new NoCons2();
    

    The order is:

    1. NoCons’s default constructor called, although this is technically the first part of (2); and then
    2. NoCons2’s default constructor called.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In our code we have quite a few cases of this pattern: class outerClass
I have few question in this regard When you create an internet page, does
I have few small basic problems : How to format : int i =
I have few questions about using lock to protect my shared data structure. I
I have few buttons of which each button is assigned a class with a
We are using JAXB to generate Java classes and have encountered a few cases
I have few similar models in Django: class Material(models.Model): title = models.CharField(max_length=255) class Meta:
I'm using C# with .net 3.5 and have a few cases where I want
I have seen in a few cases that converting a file from Microsoft Word
I have few questions about string literals in C++. char *strPtr = "Hello" ;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.