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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:43:22+00:00 2026-06-15T08:43:22+00:00

This is a snippet of Java code: static { ture = 9; } static

  • 0

This is a snippet of Java code:

static {        
    ture = 9;       
}
static int ture;
{ // instance block 
    System.out.println(":"+ture+":");           
}

How is that it compiles at all? Declaration of variable ‘ture’ has been performed after initialization. As far as I know static blocks and fields have been executed in the order they appear.

And now why is that value 9 within instance block has been printed 3 times? By the way, the instance of the class has been created 3 times.
That is not a homework, I am learning Java for certification.

  • 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-15T08:43:23+00:00Added an answer on June 15, 2026 at 8:43 am

    Regarding your first question, static blocks are indeed processed in the order in which they appear, but declarations are processed first, before the static blocks are. Declarations are processed as part of the preparation of the class (JLS §12.3.2), which occurs before initialization (JLS §12.4.2). For learning purposes, the entire JLS §12 may be useful, as well as JLS §8, particularly §8.6 and JLS §8.7. (Thank you to Ted Hopp and irreputable for calling out those sections.)

    There isn’t enough information in your quoted code to answer your second question. (In any case, on SO it’s best to ask one question per question.) But for instance:

    public class Foo {
        static {     
            ture = 9;   
        }
    
        static int ture;
    
        {   // instance block   
            System.out.println(":"+ture+":");
    
        }
    
        public static final void main(String[] args) {
            new Foo();
        }
    }
    

    …only outputs :9: once, because only one instance has been created. It doesn’t output it at all if you remove the new Foo(); line. If you’re seeing :9: three times, then it would appear that you’re creating three instances in code you haven’t shown.

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

Sidebar

Related Questions

Consider this code snippet: public static void main(String[] args) { int z1 = 0;
This is the snippet of Java code. class Test{ public static void main(String[ ]
This is a snippet of Java code: static boolean a; // gets false static
Is it possible to convert this java code snippet to php? public void testBalanceReturnsToZeroOnVending()
I have this JSP code snippet: <%@ taglib uri=http://java.sun.com/jsp/jstl/core prefix=c%> <c:choose> <c:when test=${var1.properties[\Item Type\]
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
I am beginner to java and was trying out this code puzzle from the
Let's see the following code snippet in Java. public class Main { public static
I have this snippet of java code. I am a noob in java.. Error
here's the snippet in java: class C { private static int c = 0;

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.