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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:19:29+00:00 2026-06-18T04:19:29+00:00

We all know, that according to JLS7 p.4.12.5 every instance variable is initialized with

  • 0

We all know, that according to JLS7 p.4.12.5 every instance variable is initialized with default value. E.g. (1):

public class Test {
    private Integer a;  // == null
    private int b;      // == 0
    private boolean c;  // == false
}

But I always thought, that such class implementation (2):

public class Test {
    private Integer a = null;
    private int b = 0;
    private boolean c = false;
}

is absolutely equal to example (1). I expected, that sophisticated Java compiler see that all these initialization values in (2) are redundant and omits them.

But suddenly for this two classes we have two different byte-code.

For example (1):

   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

For example (2):

   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   aconst_null
   6:   putfield    #2; //Field a:Ljava/lang/Integer;
   9:   aload_0
   10:  iconst_0
   11:  putfield    #3; //Field b:I
   14:  aload_0
   15:  iconst_0
   16:  putfield    #4; //Field c:Z
   19:  return

The question is: Why? But this is so obvious thing to be optimized. What’s the reason?

UPD: I use Java 7 1.7.0.11 x64, no special javac options

  • 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-18T04:19:30+00:00Added an answer on June 18, 2026 at 4:19 am

    No, they’re not equivalent. Default values are assigned immediately, on object instantiation. The assignment in field initializers happens when the superclass constructor has been called… which means you can see a difference in some cases. Sample code:

    class Superclass {
        public Superclass() {
            someMethod();
        }
    
        void someMethod() {}
    }
    
    class Subclass extends Superclass {
        private int explicit = 0;
        private int implicit;
    
        public Subclass() {
            System.out.println("explicit: " + explicit);
            System.out.println("implicit: " + implicit);
        }
    
        @Override void someMethod() {
            explicit = 5;
            implicit = 5;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            new Subclass();
        }
    }
    

    Output:

    explicit: 0
    implicit: 5
    

    Here you can see that the explicit field initialization “reset” the value of explicit back to 0 after the Superclass constructor finished but before the subclass constructor body executed. The value of implicit still has the value assigned within the polymorphic call to someMethod from the Superclass constructor.

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

Sidebar

Related Questions

We all know that most applications out there assume class names to follow the
Q: I know that to destroy all sessions , i can use: Session.Abandon() According
We all know that you can overload a function according to the parameters: int
We all know that openId has become such a popular method for user authentication,
We all know that CSS sprite images are great to reduce the amount of
We all know that a hash table has O(1) time for both inserts and
as you all know that if we use flash object in web page in
as we all know that html_safe work in controller and view i am sending
So we all know that when iterating through a PHP array using foreach, the
As you all might know that the MIPS instruction set supports clz (count leading

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.