1

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??

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:38

3 Answers3

3

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();
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • yes this works perfect.Thankyou:) but why is it that its not showing the error when i run it from the cmd? – Sumedha Vangury Jan 30 '13 at 13:50
  • AFAIK, it shouldn't compile in when you use command line either, if you want to access the inner class instance from a static method, you need to get it thru outer class instance. are you sure its invoking the inner class's constructor when you run it through command line ?? – PermGenError Jan 30 '13 at 13:52
  • I think it is not compulsory to instantiate an outer class to use an inner class , if it is not to be called form static method of the outer class. am I right – Abhishek Bhandari Jan 30 '13 at 13:53
  • @AbhishekBhandari absolutely, if you wanna access your inner class's memebers/methods from outer class's instance methods, you dont need the outer class instance. just creating the inner class instance would suffice, from outer class's static methods though, you need outer class's instance as said in my answer:) – PermGenError Jan 30 '13 at 13:58
  • @PremGenError yes it does work from the command line when i type javac watermark.java it does work perfectly. – Sumedha Vangury Jan 30 '13 at 14:21
0

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

Abhishek Bhandari
  • 613
  • 1
  • 5
  • 16
0

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;
        // ......
    }
}
higuaro
  • 15,730
  • 4
  • 36
  • 43