Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34026

Re: Why does Java argue about making a class instance

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Why does Java argue about making a class instance
Date 2016-03-26 16:04 -0400
Organization A noiseless patient Spider
Message-ID <nd6pps$ee3$1@dont-email.me> (permalink)
References <nd6orl$k7m$1@reader1.panix.com>

Show all headers | View raw


On 3/26/2016 3:44 PM, Popping mad wrote:
> When I try to compile this (example from antlr4 FIWIW)
> [...]
> public class Main {
>      public static void main(String[] args) {
>[...]
> 		 CollectVarsVisitor my_visitor = new CollectVarsVisitor();
> [...]
>      }
>
> 	 public class CollectVarsVisitor extends CalcBaseVisitor<Void> {
> [...]
>      	}
> [...]

> it says
>
> [ruben@manjaro calculator]$ javac Main.java
> Main.java:15: error: non-static variable this cannot be referenced from a
> static context
>                   CollectVarsVisitor my_visitor = new CollectVarsVisitor();
>                                                   ^
> 1 error
>
> I have no clue what is the problem, but it is not a standard static
> problem

     CollectVarsVisitor is an "inner class," a nested class each of whose
instances belongs to an instance of the containing class (Main).  You
cannot have a CollectVarsVisitor instance without a Main instance to
own it.  Since the main() method is static, there is no Main instance
around to be the owner of the new CollectVarsVisitor.  And that's why
the compiler complains (in its ever-so-transparent way).

     I'm not quite sure what you're trying to do, but it looks like the
solution might be to use a static nested class:

	public /* => */ static /* <= */ class CollectVarsVisitor ...

If the nested class is truly supposed to be an inner class (belonging
to a Main instance), then you'll need a Main:

	Main my_main = new Main();
	CollectVarsVisitor my_visitor
	    = my_main.new CollectVarsVisitor();

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Why does Java argue about making a class instance Popping mad <rainbow@colition.gov> - 2016-03-26 19:44 +0000
  Re: Why does Java argue about making a class instance Joerg Meier <joergmmeier@arcor.de> - 2016-03-26 21:01 +0100
    Re: Why does Java argue about making a class instance Popping mad <rainbow@colition.gov> - 2016-03-28 13:19 +0000
      Re: Why does Java argue about making a class instance Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-28 14:59 -0400
  Re: Why does Java argue about making a class instance Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-26 16:04 -0400
    Re: Why does Java argue about making a class instance Popping mad <rainbow@colition.gov> - 2016-03-28 13:24 +0000
      Re: Why does Java argue about making a class instance Arne Vajhøj <arne@vajhoej.dk> - 2016-03-28 09:50 -0400
      Re: Why does Java argue about making a class instance Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-28 15:04 -0400
        Re: Why does Java argue about making a class instance FredK <fred.l.kleinschmidt@gmail.com> - 2016-03-28 15:17 -0700
          Re: Why does Java argue about making a class instance Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-03-28 18:30 -0400
  Re: Why does Java argue about making a class instance Andreas Leitgeb <avl@logic.at> - 2016-03-26 20:17 +0000
  Re: Why does Java argue about making a class instance Arne Vajhøj <arne@vajhoej.dk> - 2016-03-28 10:10 -0400
  Re: Why does Java argue about making a class instance Roedy Green <inquiry@mindprod.com> - 2016-06-29 00:56 -0700

csiph-web