how can I trigger anything outside the method ???

like carry the boolean a outside

public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
                a=true; // cant refer to non final variable a inside an inner class defined in a different method

I want to use "a" in while loop for instance

I cant get event out of that method down,it's frustrating

here's the whole program:

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

public class WinLsnrExp extends JFrame {
    
	//private boolean a=true;
	
    public WinLsnrExp() {
        
        setTitle("Window listener Example : ");
        setSize(150, 150);
    }
    public static void main(String[] args) {
    	
    //int xI,yI;
    //double xD,yD;
    boolean a = false;	
    while (a){
    /*	for (int i=0;i<100;i++) {
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
    }
   
    }}
    	//xD=1000*Math.random();
    	//int xI = (int)xD;  
      
*/
    	
    	System.out.println(Math.random());
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
        wle.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
            }
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
                a=true; // cant refer to non final variable a inside an inner class defined in a different method
            }
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }
}
}

You need to declare a as a global variable/field.
To do so, you need to declare it outside all the methods, but still inside your class.
I saw you had declared it as a globar & private field, but you commented it.

I fixed your code, all i did was commenting the declaration of the local variable a in your main method, and uncomment the global declaration.

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

public class WinLsnrExp extends JFrame {
    
	private boolean a=true;
	
    public WinLsnrExp() {
        
        setTitle("Window listener Example : ");
        setSize(150, 150);
    }
    public static void main(String[] args) {
    	
    //int xI,yI;
    //double xD,yD;
    /*boolean*/ a = false;	
    while (a){
    /*	for (int i=0;i<100;i++) {
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
    }
   
    }}
    	//xD=1000*Math.random();
    	//int xI = (int)xD;  
      
*/
    	
    	System.out.println(Math.random());
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
        wle.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
            }
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
                a=true; // cant refer to non final variable a inside an inner class defined in a different method
            }
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }
}
}

sorry,but that wont work :)


it gives me a //cannot make a static reference to the non static field a


I've commented in code lines where it pop ups

and what is this "});" anyway?

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

public class WinLsnrExp extends JFrame {
    
	private boolean a=true;
	
    public WinLsnrExp() {
        
        setTitle("Window listener Example : ");
        setSize(150, 150);
    }
    public static void main(String[] args) {
    	
    //int xI,yI;
    //double xD,yD;
    /*boolean*/ a = false;	    //cannot make a static reference to the non static field a
    while (a){       //cannot make a static reference to the non static field a
    /*	for (int i=0;i<100;i++) {
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
    }
   
    }}
    	//xD=1000*Math.random();
    	//int xI = (int)xD;  
      
*/
    	
    	System.out.println(Math.random());
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
        wle.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
            }
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
                a=true; //cannot make a static reference to the non static field a
            }
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }
}
}

You need an instance of WinLsnrExp to use the a variable. You don't have one when you are trying to use a in main. a is not a static variable. main is a static function. Google "java static method" and "java static variable" for more information on what the word "static" means.

You need to create an instance of WinLsnrExp. Stick that code in your constructor or some other non-static function. Make main short. In particular, don't use a in main.

public WinLsnrExp()
{
    // code
}

public void dostuff ()
{
    // code using a that used to be in main
}

public static void main (String args[])
{
     WinLsnrExp wle = new WinLsnrExp ();
     wle.dostuff (); // you have an instance, so you can use a.
}

Since I don't understand what you are trying to do and what a represents, I can't give much more advice.

commented: spot on +21

oops,

I didn't checked the code carefully enough.

You need to turn your private boolean a to private static.

~ Xhamolk_

You need an instance of WinLsnrExp to use the a variable. You don't have one when you are trying to use a in main. a is not a static variable. main is a static function. Google "java static method" and "java static variable" for more information on what the word "static" means.

You need to create an instance of WinLsnrExp. Stick that code in your constructor or some other non-static function. Make main short. In particular, don't use a in main.

public WinLsnrExp()
{
    // code
}

public void dostuff ()
{
    // code using a that used to be in main
}

public static void main (String args[])
{
     WinLsnrExp wle = new WinLsnrExp ();
     wle.dostuff (); // you have an instance, so you can use a.
}

Since I don't understand what you are trying to do and what a represents, I can't give much more advice.

this is very nice instruction!!!

I've done by it and it is much better and clearer now!!!

Way to go Vernon!!!

oops,

I didn't checked the code carefully enough.

You need to turn your private boolean a to private static.

~ Xhamolk_

It wont work either way hehe

You can try if you dont believe.

This program compiles and runs. I don't think you'd ever want to write this program, but it compiles and runs.

public class SomeClass
{
    private static boolean a;
	
    public static void main (String args[])
    {
        a = false;
        System.out.println (Boolean.toString (a));
    }
}

this doesn't compile:

public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
                a=true; //cannot make a static reference to the non static field a
            }
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;

public class WinLsnrExp extends JFrame {
    
	private static boolean a=false;
	
    public WinLsnrExp() {
        
        setTitle("Window listener Example : ");
        setSize(150, 150);
    }
    public static void main(String[] args) {
    	


    	
    	System.out.println(Math.random());
        WinLsnrExp wle = new WinLsnrExp();
        wle.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        wle.setLocation((int)(Math.random()*1000),(int)(Math.random()*1000));
        wle.setVisible(true);
        wle.addWindowListener(new WindowListener() {
            public void windowClosed(WindowEvent arg0) {
                System.out.println("Window close event occur");
            }
            public void windowActivated(WindowEvent arg0) {
                System.out.println("Window Activated");
            }
            public void windowClosing(WindowEvent arg0) {
                System.out.println("Window Closing");
                a=true; //cannot make a static reference to the non static field a
            }
            public void windowDeactivated(WindowEvent arg0) {
                System.out.println("Window Deactivated");
            }
            public void windowDeiconified(WindowEvent arg0) {
                System.out.println("Window Deiconified");
            }
            public void windowIconified(WindowEvent arg0) {
                System.out.println("Window Iconified");
            }
            public void windowOpened(WindowEvent arg0) {
                System.out.println("Window Opened");
            }
        });
    }
}
}

No, it doesn't. But the reason it doesn't has nothing to do with the static variable and method. Take out the extra bracket at the end and it will compile and run. However, I still prefer the approach from post 4. I don't think a should be a static variable.

You are right!

LOL how compiler gave me this error

a=true; //cannot make a static reference to the non static


for extra }


weird

I think its due to what the compiler checks first..

Anyways, if you know the error the compiler tells you is already solver, move out and check for other type of errors.

this thread is solved!!!

thank you very much xhamolc and vernon!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.