简体   繁体   English

不能抛出私有成员类的实例? - Java

[英]Cannot throw instance of private member class? - Java

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: 它指向这一行,并在new运算符:

throw new ParenthesisException();

ParenthesisException is a private member class of the main class. ParenthesisException是主类的私有成员类。 I think the problem is probably related to that, but that's about all I can figure. 我认为问题可能与此有关,但这就是我所能想到的。

This is my definition of ParenthesisException. 这是我对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. 我将非常感谢“静态上下文”的简要说明,以及为什么new运算符不适用于我的成员类,以及如何抛出私有成员类的实例。

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. 如果我必须根据您发布的代码片段猜测发生了什么,可能是因为您尝试从static方法中抛出ParenthesisException而导致错误。

In Java, classes defined inside of another class automatically store a pointer to the object inside which they were created. 在Java中,在另一个类中定义的类自动存储指向创建它们的对象的指针。 That is ParenthesisException has an implicit pointer back to the enclosing class inside of which it was created with new . 也就是说, ParenthesisException有一个隐式指针,它返回到使用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. 这意味着,特别是,您无法在static方法内部构造new ParenthesisException ,因为没有this指针可用于引用包含的类。

To fix this, you should make ParenthesisException a static inner class like this: 要解决这个问题,你应该使ParenthesisException成为一个static内部类,如下所示:

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. private之后的static表示ParenthesisException不会将引用保存回封闭对象,这可能是您想要的。 It also means that you can create new ParenthesisException s inside of static methods. 这也意味着您可以在静态方法中创建new ParenthesisException

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. 您的main方法,并且很可能是包含main方法的类中的任何方法都是静态的,因此您不能使用非静态变量。

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. ParenthesisException是非静态的,因为您尚未将其声明为静态,但它在主类中声明并在静态方法中调用。

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'. 你只能调用静态函数,而不是'this'。 When you try to create a new exception you effectively call this.new ParenthesisException() , because the exception is a inner class. 当您尝试创建新的异常时,您有效地调用this.new ParenthesisException() ,因为异常是内部类。 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. 您需要创建主类的istance,然后在该实例上创建异常对象。

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... 如果你的main类是Animal那么你需要在main中实例化Animal ,如果你做一个简单的...

new Animal(); as the only line in main 作为唯一的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. 这很可能会解决你的问题....再次没有完整的代码这是严格的猜测,但根据你说的和错误我相信这将解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类实例作为 Java 中的私有成员 - Class instance as a private member in Java java私有类成员 - java private class member java匿名类可以访问外部类的私有成员。 为什么此代码无法访问私有数据成员? - java anoymous class can access private member of outer class. Why this code cannot access private data member? Java-私有内部类公共成员 - Java - private inner class public member Java如何访问类的私有静态成员 - Java How to access private static member of class 用于填充私有成员的 Java 回调(在另一个类中) - Java callback (in another class) to populate private member 在 Java 中的公共类中创建私有类的实例 - Making an instance of a private class in a public class in Java java.lang.IllegalAccessException:. 无法使用修饰符“private static”访问类 CrossOrigin 的成员 - java.lang.IllegalAccessException:.cannot access the member of class CrossOrigin with modifiers "private static" 为什么在 class 中的抽象 class 中有私有访问修饰符,即使我们不能创建抽象 class 的实例? - Why is there a private access modifier in an abstract class in Java, even though we cannot create an instance of an abstract class? Java问题:类的实例作为此类的静态成员 - Java question :instance of a class as a static member of this class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM