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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:17:15+00:00 2026-06-15T20:17:15+00:00

When I assign the value++ of a static int to another int, it is

  • 0

When I assign the value++ of a static int to another int, it is performing the assignments in an order that doesn’t seem to follow the order of operations for Java. Shouldn’t it do the ++ before the =?

public class Book
{
  private int id;
  private static int lastID = 0;

  public Book ()
  {
    id=lastID++;
  }
}

In the first book I construct, the id is 0. Shouldn’t it be 1 since lastID++ should happen first?

  • 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-15T20:17:16+00:00Added an answer on June 15, 2026 at 8:17 pm

    Shouldn’t it do the ++ before the =?

    –> Yes ++ is evaluated first as below :

    Your expression :

    id = lastID++;
    

    is equivalent to following expression

    temp = lastId;    // temp is 0
    lastID = lastID + 1;  // increament, lastId becomes 1
    id = temp;   // assign old value i.e. 0
    

    So you have id as 0, you should use, pre-increament operator(++) in this case as :

    public class Book
    {
      private int id;
      private static int lastID = 0;
    
      public Book ()
      {
        id = ++lastID; // pre-increament
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to assign value to an object array that is defined
To assign specific value to 1D array I'm using LINQ like so: int[] nums
When,i try to assign null value to my pointer,it sometimes crashes on that line.
A struct is a value type, so if I assign a struct to another
Possible Duplicate: default value for a static property I am able to assign default
I want to assign a value from a select box to an input field
how can I assign a value to the 'extraParams' property of the following plugin
I have to assign the value in a repeater column in such a way
I want to assign the value in QString to a const std::string QString qfile(some
can we assign null value to struct type of variable? struct MyStruct { }

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.