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

  • Home
  • SEARCH
  • 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 8263591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:09:39+00:00 2026-06-08T04:09:39+00:00

public class Test { private static final String str1 = new String(en); private static

  • 0
public class Test {

    private static final String str1 = new String("en");
    private static Test instance = initInstance();

    private static final String str2 = new String("en");
    private static final String str3 = "en";

    private Test() {
    }

    public static void main(String[] args) {
    }

    private static Test initInstance() {
        instance = new Test();
        System.out.println(str1 + ',' + str2 + ',' + str3);
        return instance;
    }
}

Theoretically with statics everywhere it should result in “en,en,en”.

Result: “en,null,en”

Expected: “en,null,null” (since i discovered statics order actually matters)

Could somebody explain this?
What is so different about “en” and new String(“en”)?

  • 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-08T04:09:42+00:00Added an answer on June 8, 2026 at 4:09 am

    Yes. At the time you invoke the method, str2 is not yet initialized (fields are initialized in order of declaration) and str3 is a compile-time constant.

    Compile-time constants are inlined by the compiler in the class file. new String(“..”) is not a constant, because it uses a constructor.

    String constants are defined by the string literal: “”, and they are placed in a string pool in the jvm instance so that they are reused. Contrary to that, using new String(..) creates a new instance, and so should be avoided.

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

Sidebar

Related Questions

public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Test { private static void funct(final int i) { new Thread(new Runnable()
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code public class Test{ private static final String key =
public class test { public static final int nThreads = 2; public static void
My code is : public class EventDataSQLHelper extends SQLiteOpenHelper { private static final String
import java.awt.*; import java.awt.event.InputEvent; import java.awt.image.BufferedImage; public final class test { private static Robot
I have a following code in test public class TestMe { private static final
I have the following code: public class Test extends JFrame implements ActionListener{ private static
public class Test extends Thread{ public void hello(String s){ System.out.println(s); } public void run(){

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.