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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:36:26+00:00 2026-05-24T16:36:26+00:00

public class Static { static { int x = 5; } static int x,y;

  • 0
public class Static
{
    static
    {
        int x = 5;
    }

    static int x,y;
    public static void main(String args[])
    {
        x--; myMethod();
        System.out.println(x + y + ++x);
    }

    public static void myMethod()
    {
        y = x++ + ++x;
    }
}

Could you please somebody help me here why it is displaying out put is 3?

  • 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-24T16:36:28+00:00Added an answer on May 24, 2026 at 4:36 pm
      static
      {
            int x = 5;
      }
    

    You redeclare x here, making it a locally scoped variable (not a class member). This assignment will have no affect whatsoever, regardless of when it is run.

    Now, you asked about the static block and that’s what I answered. If you are confused about why a value of 3 is outputted even assuming that assignment doesn’t take place, then this becomes a question about the increment operators (x++ and ++x).

    Full explanation

    I like Paulo’s explanation quite a bit but let’s just see if we can simplify the code. To start, let’s forget about making x and y a static field (making them local, initialized to the default for a static int: 0) and inline myMethod():

    int x = 0, y = 0;
    x--;
    y = x++ + ++x;
    System.out.println(x + y + ++x);
    

    First we should eliminate complex expressions. We can do that by extracting each sub-expression into a temporary variable in the correct order (expressions are evaluated left-to-right):

    int x = 0, y = 0;
    x--;
    
    int yOperand1 = x++;
    int yOperand2 = ++x;
    y = yOperand1 + yOperand2;
    
    int resultOperand1 = x;
    int resultOperand2 = y;
    int resultOperand3 = ++x;
    int result = resultOperand1 + resultOperand2 + resultOperand3;
    System.out.println(result);
    

    Now we can label the value of x, y and any temporary variables at each step:

    int x = 0, y = 0;           //x: 0   y: 0
    x--;                        //x: -1  y: 0
    
    int yOperand1 = x++;        //x: 0   y: 0  yOperand1: -1
    int yOperand2 = ++x;        //x: 1   y: 0  yOperand1: -1  yOperand2: 1
    y = yOperand1 + yOperand2;  //x: 1   y: 0
    
    int resultOperand1 = x;     //x: 1   y: 0  resultOperand1: 1
    int resultOperand2 = y;     //x: 1   resultOperand1: 1  resultOperand2: 0
    int resultOperand3 = ++x;   //x: 2   resultOperand1: 1  resultOperand2: 0  resultOperand3: 2
    int result = resultOperand1 + resultOperand2 + resultOperand3; //result: 3
    System.out.println(result);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
public class Main { public static void main(String[] args) { int[] x = {1,
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
MyClass.java: package test; public class MyClass { public void myMethod(){ System.out.println(My Method Called); }
Here is my code: public class Main { public static void main(String[] args) {
I have this piece of code: class Test { public static void main (String[]
I have the following code: public class Test { public static void Main() {

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.