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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:05:48+00:00 2026-05-26T20:05:48+00:00

The following simple program retrieves the current year (2011) from the system and then

  • 0

The following simple program retrieves the current year (2011) from the system and then simply subtracts 1950 from the current year.

package calculation;

import java.util.Calendar;

final public class Main
{
    private static final Main INSTANCE = new Main();
    private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
    private final int beltSize=CURRENT_YEAR - 1950;

    private int beltSize()
    {
        return beltSize;
    }
    public static void main(String[] args)
    {
        Main  main=new Main();
        System.out.println("Wears a size " + main.beltSize() + " belt.");
        System.out.println("Wears a size " + INSTANCE.beltSize() + " belt.");
    }
}

The following statement from the code above returns a correct value.

Main  main=new Main();

System.out.println("Wears a size " + main.beltSize() + " belt.");

It invokes the beltSize() method which returns correctly the evaluation of CURRENT_YEAR – 1950 which is 61.


private static final Main INSTANCE = new Main();

System.out.println("Wears a size " + INSTANCE.beltSize() + " belt.");

The above statement invokes the same method beltSize() using the static final object INSTANCE which is a class member and it returns incorrectly -1950 (negative) rather than the correct value of 61. Why?

  • 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-26T20:05:48+00:00Added an answer on May 26, 2026 at 8:05 pm

    Order of evaluation matters for static variables. so change the order as

    private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
    private static final Main INSTANCE = new Main();   // order of evaluation matters for static variables
    

    Since INSTANCE has evaluated before CURRENT_YEAR, the default value of CURRENT_YEAR (0) has taken.

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

Sidebar

Related Questions

I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
On Windows Server 2008 R2 the following very simple program behaves different on system
Please, observe the following simple program (based on the example from the protobuf-net project
I have this following simple service program: using System.Diagnostics; using System.ServiceProcess; namespace BasicService {
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
Based on the following simple program the bitwise left shift operator works only for
Consider the following simple C program that read a file into a buffer and
I need to write a simple program for work that does the following: read
In my simple OpenGL program I get the following error about exit redefinition: 1>c:\program
The following program is very simple: it outputs a single dot each half a

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.