0

so I have 2 classes. let's call one Main and one Sub. Main Class

 public static boolean variable; public static main(){ variable = false; sub(); while(variable == false){ //pause for 1 millisecond try { TimeUnit.MILLISECONDS.sleep(1); } catch (InterruptedException ex) { Logger.getLogger(mainGUI.class.getName()).log(Level.SEVERE, null, ex); }catch(Exception s){ JOptionPane.showMessageDialog(null, "Unexpected exception:" + s); } } System.out.println("compleate"); } 

Sub Class

import package.Main; public static sub(){ //wait for 3 seconds try { TimeUnit.MILLISECONDS.sleep(3000); } catch (InterruptedException ex) { Logger.getLogger(mainGUI.class.getName()).log(Level.SEVERE, null, ex); }catch(Exception s){ JOptionPane.showMessageDialog(null, "Unexpected exception:" + s); } Main.varaible = true; } 

I need a way to edit variables from Sub so that my program can continue.

I have tried the above Sub class but I don't think that works (i have tried testing this, and it didn't work but it was in a big program with a lot going on, so I am not sure that there wasn't another problem)

6
  • yes, I have tried, but it was in a big program and I am not entirely sure that the problem is not elsewhere. Commented Apr 30, 2020 at 0:47
  • 1
    What kind of variable? Instance or Static? Public or Private? Commented Apr 30, 2020 at 0:49
  • main is public static Commented Apr 30, 2020 at 0:50
  • 2
    Normally, if a class allows modification of it's instance variables, it exposes getter and setter methods. Commented Apr 30, 2020 at 0:51
  • 2
    Add Sub class code to your question. Commented Apr 30, 2020 at 0:52

1 Answer 1

1

You could access the variable in main if it is a public field on the class. What you have right now is a variable defined inside the scope of a function, you can't access it outside of the function

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.