开发者

Cannot throw instance of private member class? - Java

开发者 https://www.devze.com 2023-03-22 10:09 出处:网络
What does this error mean, and why does it apply? I can\'t find much info with Google about member classes and static contexts, or what those mean, in a case that seems relevant to my situation.

What does this error mean, and why does it apply? I can't find much info with Google about member classes and static contexts, or what those mean, in a case that seems relevant to my situation.

Here's the error I'm getting:

non-static variable this cannot be referenced from a static context

It points to this line, and at the new operator:

throw new ParenthesisException();

ParenthesisException is a private member class of the main class. I think the problem is probably related to that, but that's about all I can figure.

This is my definition of ParenthesisException. It is inside the main class definition: (I am sorry if the formatting is not very good)

private class ParenthesisException extends Throwable
{
    public ParenthesisException(){开发者_运维百科}
    public String strErrMsg()
    {
        return "ERROR: Every '(' needs a matching ')'";
    }
}

I find the error message rather cryptic. I would appreciate a brief explanation of "static contexts" and why the new operator isn't working for my member class, and how I can throw an instance of a private member class.


If I had to guess what's going on based on the code fragment you've posted, the error is probably caused because you're trying to throw a ParenthesisException out of a static method.

In Java, classes defined inside of another class automatically store a pointer to the object inside which they were created. That is ParenthesisException has an implicit pointer back to the enclosing class inside of which it was created with new. This means that, in particular, you cannot construct a new ParenthesisException inside of a static method, because there is no this pointer that can be used to refer to the containing class.

To fix this, you should make ParenthesisException a static inner class like this:

private static class ParenthesisException extends Throwable
{
    public ParenthesisException(){}
    public String strErrMsg()
    {
        return "ERROR: Every '(' needs a matching ')'";
    }
}

This static after the private says that ParenthesisException does not hold a reference back to an enclosing object, which is probably what you wanted anyway. It also means that you can create new ParenthesisExceptions inside of static methods.

Hope this guess is correct, and hope this helps!


The error is a bit confusing when you haven't seen it before, but it's exactly what it says: You can't use a non-static variable from a non-static context. Your main method and most likely any method in the class that contains your main method is static, so you can't use a non-static variable from it.

ParenthesisException is non-static because you haven't declared it as static, yet it is declared within your main class and called within a static method.

Don't declare this as an inner-class -- make your a new public class. You'll then be able to instantiate this exception.


Your main function is 'static context'. You can only call static function and there is not 'this'. When you try to create a new exception you effectively call this.new ParenthesisException(), because the exception is a inner class. But you can't do that. You need to create an istance of your main class and then create the exception object on that instance.

MainClass m = new MainClass();
throw m.new ParenthesisException();

Another solution is to define your exception as static class:

private static class ParenthesisException extends Throwable

Then you can instantiate it from static contexts, so you don't need an instance of the main class.


Without code this is strictly a guess, but based on the error message I believe this would work. If your main class is Animal then you need to instantiate Animal so inside of main if you do a simple...

new Animal(); as the only line in main

then make a new method....

public Animal(){
    /* put code that used to be in main */
}

This will most likely fix your problem....again without full code this is strictly a guess, but based on what you said and the error I believe this will solve the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消