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 8022817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:22:52+00:00 2026-06-04T22:22:52+00:00

In the constructor below, I have initialized only two of the variables, leaving some

  • 0

In the constructor below, I have initialized only two of the variables, leaving some variables uninitialized explicitly.

As i have read, no argument constructor is created by the compiler if the constructor is provided by us.Then in such cases, since I have my own constructor so there is no default constructor which initializes variables p and q.

So, the logic should be if we try to access those uninitialized variables, then it should be a compile time error.However, the following code runs successfully.

The output is
5
10
0.0
0.0

How can we explain the output 0.0 and 0.0 since i have not declared them in the constructor ??

public class Rectangle {
int l, b;
double p, q;

public Rectangle(int x, int y) {
l = x;
b = y;
}

public static void main(String[] args) {
    Rectangle obj1= new Rectangle(5,10);
    System.out.println(obj1.l);
    System.out.println(obj1.b);
    System.out.println(obj1.p);
    System.out.println(obj1.q); 
}

}

  • 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-06-04T22:22:53+00:00Added an answer on June 4, 2026 at 10:22 pm

    Primitives are initialized to a default value, in this case, 0 for integers and 0.0 for floats or doubles. Objects are by default null, for example a String would be null.

    The values you set in constructors are normally different from the default values, therefore the need for constructors.

    The default initial values are a language feature, and does not require a constructor at all.

    You read about the default values here : http://www.janeg.ca/scjp/lang/defaults.html

    Remember though that the default values are only for CLASS members, not local variables. That difference is why you get compile errors for some values not being initialized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is really two questions, noted below: Currently I have some public internal helper
I have problem in below code. I have created two methods ENCRYPT and DECRYPT
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
In the class below, only the second constructor takes a ForumThread object. Otherwise, it
I have tried to use C++0x initializer list as argument to a constructor call
I have a constructor that takes some arguments. I had assumed that they were
I wonder if there is something wrong with the copy constructor function below? class
The below code calls the ReadXMLFile constructor ReadXMLFile r=new ReadXMLFile(small.xml); HashMap r=r.getrcset(); I am
Below I am creating an object in JavaScript. Within the constructor I am setting
Why is the Child class's converter constructor called in the code below? I mean,

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.