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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:37:46+00:00 2026-05-31T10:37:46+00:00

Can anyone explain me execution flow for the following java code ?? Sorry for

  • 0

“Can anyone explain me execution flow for the following java code ??”
Sorry for my misleading statement… my question is …

Main b = new Main();

control goes to the A class constructer, then control directly goes to the overriden method of class Main

public void PrintFields() {
    System.out.println("x = "+x+" y = "+y);
}

Why compiler doesn’t give error because x and y is not created..!! i am confused in this only.

When will x and y created in memory and when its initialization takes place ?? is the x and y created when control reaches to following statements in Main class ?

int x = 1;
int y;

Code:

class A
{
    public A() {
        PrintFields();
    }
    public void PrintFields() {}
}
class Main extends A
{
    int x = 1;
    int y;
    public Main() {
        y = -1;
        PrintFields();
    }
    public void PrintFields() {
        System.out.println("x = "+x+" y = "+y);
    }
}
class Test
{
    public static void main(String[] args) {
        Main b = new Main();
    }
}

Output:

run:
x = 0 y = 0 //why 0 0 ?
x = 1 y = -1 // why 1 -1 ?
BUILD SUCCESSFUL (total time: 0 seconds)

Thank you.

  • 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-31T10:37:47+00:00Added an answer on May 31, 2026 at 10:37 am
    Main b = new Main();
    

    control goes to the A class constructer, then calls the overriden method of class Main

    public void PrintFields() {
        System.out.println("x = "+x+" y = "+y);
    }
    

    At this time x and y have their default values

    so x=0, y=0
    

    Now control comes to

    int x = 1;
    int y;
    public Main() {
        y = -1;
        PrintFields();
    }
    

    and then

    public void PrintFields() {
        System.out.println("x = "+x+" y = "+y);
    }
    

    at this time x =1 and y=-1

    so output is

    x=1, y=-1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My java Fx webview throws JS exception for following code. Can anyone explain me
Can anyone explain why following code won't compile? At least on g++ 4.2.4. And
Can anyone explain why the following happens: My ASP.NET application requires access to Application
I'm using the code below and receiving the FileNotFound exception. Can anyone explain why
Can anyone explain why the following (using the System namespace qualifer) works: Add-Type @
Can anyone explain me what is the difference between execution plan and explain plan.
Can anyone explain me why we aren't dereferencing a pointer now to an NSDate
Can anyone explain what this mod_rewrite rule is doing? I'm trying to comment the
Can anyone explain what advantages there are to using a tool like MSBuild (or
Can anyone explain in simple words what First and Second Level caching in Hibernate/NHibernate

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.