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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:56:37+00:00 2026-05-22T20:56:37+00:00

I have this class, let’s say, Foo . It extends JFrame and is a

  • 0

I have this class, let’s say, Foo. It extends JFrame and is a singleton. That being said, it has two static fields: 1) an instance of Foo and 2) a Color.

Here’s a code snippet of Foo:

public class Foo extends JFrame{
    private static final Color FOO_RED = new Color(155, 64, 69);
    private static final Foo INSTANCE = new Foo();

    private Foo(){
        //do stuff
    }

    public static Foo getInstance(){
        return INSTANCE;
    }
}

I also have another class, let’s say, Launcher. This is the main class that is responsible for launching the application. It’s a simple class in that its only job is to delegate the task of constructing Foo to the EDT.

Here’s a code snippet of Launcher:

public class Launcher{
    public static void main(String[] args){
        SwingUtilities.invokeLater((new Runnable(){
            @Override
            public void run()
            {
                Foo.getInstance();
            }
        }));
    }
}

Now, this all works just fine. But, when I switch the ordering of Foo‘s fields (See below), the components that use FOO_RED are no longer painted this color.

public class Foo extends JFrame{
    private static final Foo INSTANCE = new Foo(); //declared before Color
    private static final Color FOO_RED = new Color(155, 64, 69);

    private Foo(){
        //do stuff
    }

    public static Foo getInstance(){
        return INSTANCE;
    }
}

So, this begs the question, does the ordering of static fields matter when it comes to Swing?

  • 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-22T20:56:37+00:00Added an answer on May 22, 2026 at 8:56 pm

    As already mentioned, ordering of static fields does matter. Executed in the order they appear.

    I would make another change to this example. This would make you static field order less important.

    UPDATE: Use IDOH (Initialization on Demand Holder) pattern to make singleton thread safe.

    private static class FooHolder {
        private static final Foo INSTANCE = new Foo();
    }
    
    public static Foo getInstance(){
        return FooHolder.INSTANCE;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
Let's say I have a class that implements the IDisposable interface. Something like this:
Let's say I have this class Logger that is logging strings in a low-priority
Let's say I for example have this class that generates Fibonacci numbers: public class
Let's Say I Have This Class: package{ import flash.display.Sprite; public class Main extends Sprite{
Let's say I have an abstract base class that looks like this: class StellarObject(BaseModel):
I know python functions are virtual by default. Let's say I have this: class
Let's say I have this : class whatever(object): def __init__(self): pass and this function:
Let's say I have this class: public abstract class CustomerCollectionBase : Collection<Customer>{} One of

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.