简体   繁体   中英

Error in eclipse:unresolved compilation

i have this code

public class watermark {

    public static void main(String[] args) {
        wmmain m = new wmmain();
        m.setSize(800, 500);
        m.setVisible(true);
    }

    class wmmain extends JFrame /* MAIN WINDOW */
    {
        JMenuBar jmb;
        // ......
    }
}

It works fine from the command prompt but when i try to run the code in eclipse it gives me the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
No enclosing instance of type watermark is accessible. Must qualify the allocation with an enclosing instance of type watermark (e.g. x.new A() where x is an instance of watermark).

at watermark.main(watermark.java:20)

What should i do??

From Documentation :

To instantiate an inner class, you must first instantiate the outer class.

syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

you need Outer class instance to create an instance of your Inner class.

wmmain m=new WaterMark().new wmmain();

I think you cannot call innerClass inside form Static method of its own.

import javax.swing.JFrame;
public class testFrame {
public static void main(String[] args) {
    // TODO Auto-generated method stub


}
private void getMe(){
    wmmain m = new wmmain();
    m.setSize(800,500);
    m.setVisible(true);
}
class wmmain extends JFrame /* MAIN WINDOW */
{
    public wmmain(){

    }
     }
     }

Try this . it will work . or you can use the answer of PermGenError

If the wmmain class is not using member variables from watermark and you want to hide the window class, then you could also declare wmmain as a private static inner class:

public class watermark {
    public static void main(String[] args) {
        wmmain m =  new wmmain();
        m.setSize(800, 500);
        m.setVisible(true);
    }

    private static class wmmain extends JFrame {
        JMenuBar jmb;
        // ......
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM