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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:30:20+00:00 2026-05-23T18:30:20+00:00

My reading of JLS 12.5 makes me think the assertion in the code sample

  • 0

My reading of JLS 12.5 makes me think the assertion in the code sample should never trigger—but it does in my multithreaded code. (The JLS doesn’t specify threading in the section). But whether or not my reading is correct is beside the point. I want to get this to always be true.

public class MainWindow extends JFrame {
  private final JLabel label;

  public MainWindow() {
    label = new JLabel();
    pack();
    setVisible(true);
  }

  public JLabel getLabel() {
    Assert.assertNotNull(label);
    return label;
  }
}

The obvious answer is to wrap the innards of the constructor in a synchronized block and to mark the getter synchronized as well. Is there a better way?

FWIW, the other thread is getting a reference to the window with this code inside a junit test:

private MainWindow findMainWindow() {
    for (Frame frame : Frame.getFrames()) {
        if (frame instanceof MainWindow) {
            return (MainWindow)frame;
        }
    }
    return null;
}

(BTW, I’m running JDK6 on a Mac)

Update:

I even tried synchronizing it and it still doesn’t work. Here’s the code:

public class MainWindow extends JFrame {
  private final JLabel label;
  public MainWindow() {
    synchronized(this) {
        label = new JLabel();
    }
  }

  public synchronized JLabel getLabel() {
    Assert.assertNotNull(label);
    return label;
  }
}

Update 2:

Here’s the change that fixed it:

private MainWindow findMainWindow() throws Exception {
    final AtomicReference<MainWindow> window = new AtomicReference<MainWindow>();
    SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
            for (Frame frame : Frame.getFrames()) {
                if (frame instanceof MainWindow) {
                    window.set((MainWindow) frame);
                    return;
                }
            }
        }
    });
    return window.get();
}
  • 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-23T18:30:21+00:00Added an answer on May 23, 2026 at 6:30 pm

    (Note: if one of the originators of this answer actually answers it, I will un-accept this answer and accept theirs instead.)

    The window frame is being constructed off the EDT. This against Swing policy where all access—including creation—should happen on the EDT.

    I now create the window like this (using my EventQueue utility class):

    MainWindow ui = EventQueue.invokeAndGet(new Callable() {
        public MainWindow call() {
            return new MainWindow(...);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading source code of sample projects, such as Beast and Bort, are recommended as
Reading the documentation at http://jersey.java.net/nonav/documentation/latest/client-api.html#d4e704 makes me think that ClientFilter s are processed in
Reading this question I found this as (note the quotation marks) code to solve
Reading source code of my current project, I see: [self retain] in one class,
Reading code from other posts, I'm seeing something like this. struct Foo { Foo()
Reading through more SICP and I'm stuck on exercise 1.3.8 . My code works
Reading the FTP RFC (RFC959), I notice some modes that I've never seen used,
Reading the question Why doesn’t C++ STL support atoi(const string& ) like functions? ,
I am reading the JLS spec on memory model, 17.4.5 Happens-before Order . I
I'm reading this: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2 They say: Consider the example program: class Point { int

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.