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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:07:06+00:00 2026-05-22T12:07:06+00:00

public class Main { static final int alex=getc(); static final int alex1=Integer.parseInt(10); static final

  • 0
public class Main {
 static final int alex=getc();
 static final int alex1=Integer.parseInt("10");
 static final int alex2=getc();

public static int getc(){
    return alex1;
}

public static void main(String[] args) {
    final Main m = new Main();
    System.out.println(alex+" "+alex1 +" "+alex2);
  } 
}

Can someone tell me why this prints: 0 10 10? I understand that it’s a static final variable and its value shouldn’t change but it`s a little difficult to understand how the compiler initializes the fields.

  • 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-22T12:07:07+00:00Added an answer on May 22, 2026 at 12:07 pm

    This situation is covered by JLS 8.3.2.3 “Restrictions on the use of Fields during Initialization”.

    The JLS rules allows the usage in your Question, and state that the first call to getc() will return default (uninitialized) value of alex.

    However, the rules disallow some uses of uninitialized variables; e.g.

    int i = j + 1;
    int j = i + 1;
    

    is disallowed.


    Re some of the other answers. This is not a case where the Java compiler “can’t figure it out”. The compiler is strictly implementing what the Java Language Specification specifies. (Or to put it another way, a compiler could be written to detect the circularity in your example and call it a compilation error. However, if it did this, it would be rejecting valid Java programs, and therefore wouldn’t be a conformant Java compiler.)


    In a comment you state this:

    … final fields always must be initialized at compile or at runtime before the object creation.

    This is not correct.

    There are actually two kinds of final fields:

    • A so-called “constant variable” is indeed evaluated at compile time. (A constant variable is a variable “of primitive type or type String, that is final and initialized with a compile-time constant expression” – see JLS 4.12.4.). Such a field will always have been initialized by the time you access it … modulo certain complications that are not relevant here.

    • Other final fields are initialized in the order specified by the JLS, and it is possible to see the field’s value before it has been initialized. The restriction on final variables is that they must be initialized once and only once during class initialization (for a static) or during object initialization.


    Finally, this stuff is very much “corner case” behavior. A typical well-written class won’t need to
    access a final field before it has been initialized.

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

Sidebar

Related Questions

package hw3; public class Main { public static void main(String[] args) { final int
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
The following code does not compile: public class GenericsTest { public static void main(String[]
public class Main { public static void main(String args[]) { List list = new
import java.util.Arrays; import java.util.*; class Main { public static void main(String[] args) { }
I have the following code: public class Test { public static void Main() {
Here's my code: class Ramka extends JFrame { public static final int SZEROKOSC =

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.