Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34041
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Why does Java argue about making a class instance |
| Date | 2016-03-28 10:10 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <ndbe04$rj9$1@gioia.aioe.org> (permalink) |
| References | <nd6orl$k7m$1@reader1.panix.com> |
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
Other have already pointed out the problem.
But let me try and elaborate.
Or maybe add to the confusion.
:-)
When a class is declared inside another class with static then
it is just another class with an unusual name and more complex
accessibility.
Example:
public class Main1 {
// note static
public static class Foobar {
@Override
public String toString() {
return super.toString();
}
}
public void test() {
// full syntax
Main1.Foobar a = new Main1.Foobar();
System.out.println(a);
// abbreviated syntax
Foobar b = new Foobar();
System.out.println(b);
}
public static void main(String[] args) {
Main1 o = new Main1();
o.test();
}
}
When it is declared without static, then each instance
belong to an instance of the outer class. And in fact code
can refer to that using classname.this syntax.
Example:
public class Main2 {
// note not static
public class Foobar {
@Override
public String toString() {
return super.toString() + "/" + Main2.this.toString();
}
}
public void test() {
// full syntax
Main2.Foobar a = this.new Foobar();
System.out.println(a);
// abbreviated syntax
Foobar b = new Foobar();
System.out.println(b);
}
public static void main(String[] args) {
Main2 o = new Main2();
o.test();
}
}
(and your problem as pointed out by several other was that you had
this case and were using the abbreviated syntax with an implicit this
prefix on new - an getting the error because in the static main there
is no this)
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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