0

A previously question on static references was closed as vague. In any event, I now know the the answer (and will hopefully remember the "solution"), but what is this technique called?

The most popular answer states:

Your instance variable cannot be referenced from a static context. You need an object of the class to get (a reference to) it's contents.

This question has been asked before. Surely there's a catch-phrase for this approach?

Community
  • 1
  • 1
Thufir
  • 8,216
  • 28
  • 125
  • 273

1 Answers1

1

The problem with your approach is that you have a class that doesn't look like it needs any static members, but you are trying to work in a static context because a static main method gets Dr.Frankensteined onto the class.

Don't do that. Just put main on another class instead. The accepted answer to that other question is good for a quick and dirty solution, but it's quite dirty.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • It's just something I've seen my instructor do, arguing that the class is reusable because the methods and fields aren't static; also some Swing generated classes do this. – Thufir Mar 21 '13 at 00:05
  • 1
    The class is reusable in the same sense that a car with a sofa strapped on top is perfectly usable for road travel. That still doesn't mean you should design a factory model that way. – Jon Mar 21 '13 at 09:21
  • LOL. Ok, just confirming much of the "advice" that my instructor gives is suspect. – Thufir Mar 21 '13 at 09:49