Can't start an instance of a private class? - Java

What does this error mean and why is it applied? I cannot find much information about Google about member classes and static contexts, or what they mean in a case that seems appropriate to my situation.

Here is the error I get:

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

It points to this line and to the new operator:

throw new ParenthesisException();

ParenthesisException is a private member class of the main class. I think the problem is probably related to this, but this is about everything I can understand.

This is my definition of ParenthesisException. It is inside the definition of the main class: (sorry if 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 the โ€œstatic contextsโ€ and why the new operator does not work for my member class and how I can throw an instance of a private member class.

+4
source share
4 answers

If I had to guess what was happening based on the piece of code you posted, the error is probably caused by the fact that you are trying to throw a ParenthesisException from the static method.

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

To fix this, you should make the inner class ParenthesisException a static as follows:

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

This static after private says that ParenthesisException does not contain a link back to the object object, which you probably want anyway. It also means that you can new ParenthesisException inside static methods.

Hope this assumption is correct, and hope this helps!

+6
source

The error is somewhat confusing if you have not seen it before, but this is exactly what it says: you cannot use a non-stationary 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 cannot use a non-stationary variable from it.

ParenthesisException is not static because you did not declare it as static, but it is declared in your main class and called in the static method.

Do not declare this as an inner class - create a new public class. You can then instantiate this exception.

0
source

Your main function is the "static context". You can only call a static function, not 'this'. When you try to create a new exception, you actually call this.new ParenthesisException() , because this exception is an inner class. But you cannot do that. You need to create the status of the main class, and then create the exception object in this instance.

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

Another solution is to define your exception as a static class:

 private static class ParenthesisException extends Throwable 

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

0
source

Without code, this is strictly a guess, but based on the error message, I believe this will work. If your main class is Animal , then you need to create an instance of Animal inside the main one, if you make it simple ...

new Animal(); as the only line in main

then create a new method ....

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

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

0
source

All Articles