简体   繁体   中英

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

public static void main(String[] args) {

Scanner input = new Scanner(System.in);
System.out.println("Enter expression: ");
String exp = input.nextLine().trim();

ExpressionTree exptree = new ExpressionTree1(); //new instance of ExpressionTree
TreeNode root = new TreeNode();
root = exptree.parseOutsideExpression(exp);
int evaluate = exptree.Compute(root);
String answer = Integer.toString(evaluate);
System.out.println("The evaluated expression is:" + answer);

}

class ExpressionTree{

I've looked at several examples and I've noticed that a common answer is to create a new instance of your object within main() which I've done, but I still get the same error and I am unclear as to why.

If ExpressionTree is an inner class of the same class with the main() method, maybe the problem is that it's not static. Non-static inner classes can only be instantiated in non-static code... see:

Java inner class and static nested class

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