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

  • Home
  • SEARCH
  • 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 7939807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:12:56+00:00 2026-06-03T23:12:56+00:00

Given this Java code, this outputs 0 and 4 : class A{ A() {

  • 0

Given this Java code, this outputs 0 and 4:

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

class B extends A{
   int i =   Math.round(3.5f); 
    
   public static void main(String[] args){
      A a = new B();
      a.print();
   }
   void print() { System.out.println(i); }
}

And with this identical C# code, this outputs 4 and 4

using System;

class A{
   internal A() {  print();   }
   virtual internal void print() { Console.WriteLine("A"); }
}

class B : A{
   int i =  (int) Math.Round(3.5f); 
    
   public static void Main(string[] args){
      A a = new B();
      a.print();
   }
   override internal void print() { Console.WriteLine(i); }
}

Though I figure out that the output should be 4 and 4 on Java, but the answer is actually 0 and 4 on Java. Then I tried it in C#, the answer is 4 and 4

What gives? Java rationale is, during construction of B, A is still initializing(consequently I posit B is still initializing if Java said A is still initializing), so the default value should be 0. Hence the output is 0 and 4 in Java.

Why does C# constructor behavior differ from Java, or vice versa?

  • 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-03T23:12:57+00:00Added an answer on June 3, 2026 at 11:12 pm

    It’s happening because of differences in the ordering of object initialisation in constructors.

    What is happening in Java:

    • (empty, implicit) Constructor of B is called
    • Superclass Construtor of A is called (prints 0 since i is uninitialised)
    • i is initialised after the superclass constructor
    • print() is called (prints 4)

    What is happening in C#:

    • (empty, implicit) Constructor of B is called
    • i is initialised before calling the superclass constructor
    • Superclass Construtor of A is called (prints 4 since i is already initialised)
    • print() is called (prints 4)

    Neither is right or wrong – it’s just a difference in how the compiler orders the construction operations. Personally I think the Java ordering is a marginally more logical, because it makes sense to me that the superclass is fully constructed before the subclass initialisation takes place.

    Either way, because the logic can get quite complicated, I’d suggest that you avoid calling virtual methods during object construction in general.

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

Sidebar

Related Questions

This Java code: public class XYZ { public static void main(){ int toyNumber =
Given this code: class Overloading extends Object { static public void target(Object val, String
The code: import java.math.*; public class x { public static void main(String[] args) {
Given a line number of a particular class source code (Java/C#) - is there
Consider this Java code class A{ //@returns Class object this method is contained in
I am not sure why this code (gives stackoverflow at runtime) compiles: import java.io.*;
I was testing Thrift on java, but this code give me some errors.. ClientVoiceToTextHandler
I would greatly appreciate if you could help me with this in Java. Given
This loop is strange. I ran this in Java, it gives an Index out
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker

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.