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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:14:40+00:00 2026-05-29T09:14:40+00:00

I have written a piece of code : public class Child{ int y ;

  • 0

I have written a piece of code :

public class Child{
int y ;
private static final int z = getZ();

static {
    System.out.println("The value of z is "+z);
}

public int getX(){
    System.out.println("get x");
    return 10;
}

public int getY(){
    Child ch = new Child();
    System.out.println("get y");
    ch.y = getX();
    return y;
}

public static int getZ(){
    System.out.println("get z");
    return new Child().getY();
}

public Child(){
    System.out.println("Child constructor");
}


public static void main(String...args){
    Child ch = new Child();
    System.out.println("the value of z in main is "+z);
}
}

And the output is :

get z
Child constructor
Child constructor
get y
get x
The value of z is 0
Child constructor
the value of z in main is 0

Can anyone please explain me why the value of z is 0 and not 10 ?

EDIT:- Thanks everyone , I got the answer to my first question . I still have a doubt , as far as I know the static blocks are executed after the class is loaded and before the first object of the class is instantiated . Well then the SOP(“The value of z is “+z) should have been executed before SOP(“Child constructor”) ! Ain’t it ?

  • 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-29T09:14:41+00:00Added an answer on May 29, 2026 at 9:14 am

    Look at getY():

    public int getY(){
        Child ch = new Child();
        System.out.println("get y");
        ch.y = getX();
        return y;
    }
    

    The first three lines are irrelevant – they don’t change the value of y in this instance, which is what gets returned.

    You’re creating an awful lot of pointless objects in frankly spaghetti code, called while initializing the same class that you’re constructing instances of. I suggest you try to keep your code a lot simpler than this. Static initializers should be avoided where possible to start with, let alone ones that go all round the houses to do no useful work.

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

Sidebar

Related Questions

I have written this piece of code public class Test{ public static void main(String[]
I have written a piece of code and it runs fine on my system.
I have written a piece of code which uses System.ServiceModel.Syndication library to parse RSS
I have a piece of performance critical code written with pointers and dynamic memory.
I have written this piece of code that splits a string and stores it
In jQuery, I have written a piece of code that appears on every page
I have written this piece of CF code to get and display the data
I have written the following piece of code if( (!isset($_SESSION['home'])) || (!isset($_SESSION['away'])) ) I
I have this piece of code for reading from an excel cell: public T
I have written the following piece of code that reads through a given xml-file

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.