Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What does the following error message mean: Can make a static reference to nonstatic variable?

0
Posted

What does the following error message mean: Can make a static reference to nonstatic variable?

0

Ans : The compiler is complaining that you are trying to reference the implicit instance variable this from within a class method. The Java language lets you define three kinds of variables: class variables, instance variables, and local variables (Q1.5). Class variables are defined on a per-class basis, and all instances of a class can access them. Instance variables are defined on a one-per-object basis, and you can access them only in connection with some specific instance of a class. Local variables are defined inside individual methods and can be accessed only from inside that same method. The “Can’t make a static reference …” error message most commonly arises in a class’s main(String[]) method, which the Java language requires to be a class method (declared with the static keyword; see Q1.6). The following code exemplifies the error: /* The compiler will complain about this: */ class StaticError { String myString = “hello”; public static void main(String[] args) { System.out.p

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123