We help IT Professionals succeed at work.

compilation error when i insert SOP in addWindowListener

Chandramouli k
on
Hi Experts

Why am getting compilation error when i insert SOP in addWindowListener? Any reason.

Appricaite if you could also provide references?

 myFrame.addWindowListener(new WindowAdapter()
            {            
               //System.out.println("hi");       --- getting compilation error
                public void windowClosing(WindowEvent e)
                {
                System.out.println("hi");      
                    System.exit(0);
                }
            });
Comment
Watch Question

Mick BarryJava Developer
Top Expert 2010

Commented:
you need to add it inside a method

Top Expert 2009
Commented:
myFrame.addWindowListener(new WindowAdapter()
{  --> Here you are starting an anonymous inner class
     //System.out.println("hi");       --- getting compilation error --> because this is a dangling statement inside the class
     public void windowClosing(WindowEvent e)
                {
                System.out.println("hi");      
                    System.exit(0);
                }
            });
I guess, the above code does not have error. Please share your full code.
Also, the exact erro message.
Sorry, i misunderstood the problem.

Yes, you can not use directly SOP inside the class WindowAdapter
you should use inside a block/method
Chandramouli kArchitect

Author

Commented:
Thanks for your replies.

pramod / objects:
from the compilation error i understood i cannot/shouldnt use but why is my question... Could you elaborate more as why we cannot use?

a_b
>>getting compilation error --> because this is a dangling statement inside the class?

what does it mean? Is this is same as class that i can only declare variables.
a_b
Top Expert 2009

Commented:
Let's say you have a class -

public class A
{
   private int a;
 
   sysout("Hello");     --> This is neither a variable declaration or within any method
   
  public int getA()
   {  return a};
   public void setA(int num)
   {  a = num};
Mick BarryJava Developer
Top Expert 2010
Commented:
> what does it mean? Is this is same as class that i can only declare variables.

correct, what you are deifing is in fact a class, an anonymous inner class
Chandramouli kArchitect

Author

Commented:
Thanks a_b / objects.
have started reading anonymous inner class in detail.
Chandramouli kArchitect

Author

Commented:
this link is useful as well
>> http://www.tek-tips.com/faqs.cfm?fid=1849