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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:24:02+00:00 2026-06-12T18:24:02+00:00

For the life of me, I cannot figure out why this program does not

  • 0

For the life of me, I cannot figure out why this program does not work in Java 7. I’ve run it with no problems in using Java 6, but as soon as I run it with Java 7, it doesn’t work.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class HelloWorld implements ActionListener {

JButton button;
boolean state;

public HelloWorld(){
    init();
    state = false;
    System.out.println("state - "+state);

    while (true){
        if (state == true){
            System.out.println("Success");
        }
    }
}

private void init(){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button = new JButton("Button");
    button.addActionListener(this);
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    JButton source = (JButton)e.getSource();

    if (source == button){
        state = !state;
        System.out.println("state - "+state);
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    new HelloWorld();
}

}

Using Java 6, if I press the button, it will print out the phrase “Success” until I hit the button again. Using Java 7 registers that the button was pressed and the value of state was changed to true, but the phrase “Success” is never printed. What’s going on?

  • 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-12T18:24:03+00:00Added an answer on June 12, 2026 at 6:24 pm

    Add volatile to the field declaration.

    Without volatile, changes in the field are not guaranteed to be visible on other threads.
    In particular, the JITter is free to believe that the field never changes on the main thread, allowing it to remove the if entirely.

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

Sidebar

Related Questions

spent a few hours trying to figure this out, but cannot for the life
I cannot work this out to save my life! So, I have a Codeigniter
I understand BFS, and DFS, but for the life of me cannot figure out
I just cannot for the life of me figure out this memory leak in
For the life of me, I cannot figure out why this NSTimer wont fire.
For the life of me, I cannot figure out the cause of this: when
For the life of me I cannot figure out why this is happening to
For the life of me, I cannot figure out anything about this topic. Maybe
For the life of me I cannot figure out what is wrong with this
I cannot for the life of me figure out why I am getting this

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.