-1

There's a particular idiom to putting a method, or perhaps anonymous inner class, somehow, into the main method of a driver class:

package net.bounceme.dur.misc;

import net.bounceme.dur.misc.Foo;

public class StaticRef {

    Foo f = Foo.INSTANCE;

    public static void main(String[] args) throws Exception {

        f.connect();  //move this to inside "run"
        /*
        public void run(){
           StaticRef sf = new StaticRef();
           //do stuff here
        }
         */
    }
}

to prevent the below error:

init:
Deleting: /home/thufir/NetBeansProjects/nntp/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/nntp/build/built-jar.properties
Compiling 1 source file to /home/thufir/NetBeansProjects/nntp/build/classes
/home/thufir/NetBeansProjects/nntp/src/net/bounceme/dur/misc/StaticRef.java:11: non-static variable f cannot be referenced from a static context
        f.connect();  //move this to inside "run"
1 error
/home/thufir/NetBeansProjects/nntp/nbproject/build-impl.xml:626: The following error occurred while executing this line:
/home/thufir/NetBeansProjects/nntp/nbproject/build-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

However, I can't find anything so far. I see something similar in threading examples, but can't quite get the syntax.

The following is largely what I want, but I don't think it's quite correct:

package net.bounceme.dur.misc;

public class StaticRef {

    Foo f = Foo.INSTANCE;

    public static void main(String[] args) throws Exception {

        StaticRef sf = new StaticRef();
        sf.f.connect();
    }
}

What I would like is to put the instantiation of sf into...I'm not quite sure. Maybe the above is correct and "ok"?

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • In your example, Foo is not static. f.connect() will not work. Is your question "How do I call f.connect() inside public static main?". If so, you can't without creating an instance of StaticRef. – kufudo Feb 18 '13 at 07:13
  • Pardon, yes, Foo is not static, nor do I wish it to be so. What's the work-around to instantiating Foo without declaring it to be static or final? I'll revise the question, I might've gotten mixed up. – Thufir Feb 18 '13 at 07:19
  • 2
    @Thufir. After your updated question, I really don't understand now what you want. Because your updated code will work fine. – Rohit Jain Feb 18 '13 at 07:19
  • static missing in front of `Foo f = ` to allow writing `sf.f.connect()`... – Aubin Feb 18 '13 at 07:23
  • Only it's a bit awkward to reference the Foo instance, which I'd rather not be either static nor final, only private. I believe there's an idiom which "runnable" uses, and I've seen it in Swing generated code as well. – Thufir Feb 18 '13 at 07:23
  • possible duplicate of [non-static variable cannot be referenced from a static context (java)](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context-java) – user207421 Feb 18 '13 at 09:15
  • @EJP I don't think it's not a real question because it has a real answer. I was asking for the work around, and found it -- see my answer. Answers like "declare variable static" were clearly not what I was asking. Please do **comment** on my answer rather than just downvoting. – Thufir Feb 18 '13 at 10:33
  • @Thufir: Don't jump to conclusions: If you look at EJP's profile you'll see no downvotes have been made today. Marking a question as a dup is fair comment. – Jon Egerton Feb 18 '13 at 10:51
  • @JonEgerton I did jump to conclusions, I didn't know you could look at downvotes like that. It's just annoying in that there 20million questions on that error message, and, here and there, you can find the answer I was looking for -- but it's a real needle in a haystack. In fact, I referenced my sources in my answer for that reason. I didn't find the answer in a question, but saw a few things which triggered recalling this "idiom", if it is such. Is it an idiom? As best I could I crafted a careful question on a topic with, admittedly, bazillions of duplicate questions. Sorry EJP. – Thufir Feb 18 '13 at 11:00

4 Answers4

1

You have some options:

  • Move Foo into the scope of main
  • Declare Foo as a static variable

    static Foo f = Foo.INSTANCE;
    
  • Create an instance of StaticRef and use the object from that

    new StaticRef().f.connect();
    
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • only for the second option, referencing the Foo object f is a bit awkward. – Thufir Feb 18 '13 at 07:25
  • If it's public, protected or package visibility, it's possible - but I do agree a bit awkward. Proper OO style would indicate that a getter be used for it (`getFoo()`), but if that's how it's done, then... – Makoto Feb 18 '13 at 07:26
1

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.

You can write a Singleton pattern:

public class SingletonDemo {
    private static SingletonDemo instance = null;

    private SingletonDemo() {       }

    public static SingletonDemo getInstance() {
            if (instance == null) {
                instance = new SingletonDemo ();
            }
            return instance;
    }

}

If thread safety is an issue, you can use an enum:

public enum Singleton{
    INSTANCE;
    private Singleton(){ ... }
}

or

public class Singleton{
    private final static Singleton instance = new Singleton();
    private Singleton(){ ... }
    public static Singleton getInstance(){ return instance; }
}

See here

Community
  • 1
  • 1
Steven
  • 1,365
  • 2
  • 13
  • 28
1

Sorry, can't comment yet so I have to post it as an answer.

The updated code is fine, but since Foo.INSTANCE seems to be a use of the Singleton pattern, it makes sense to define it as static and use it as in your first example. At most there will be a single value of Foo.INSTANCE, so it doesn't make sense to make each instance of StaticRef to have its own reference to Foo.INSTANCE.

Javier
  • 12,100
  • 5
  • 46
  • 57
0

If this code is "faulty" or violates any OOP principle, please do comment:

package net.bounceme.dur.misc;

public class StaticRef {

    private Foo f = Foo.INSTANCE;

    public StaticRef() throws Exception {
        f.connect();
    }

    public static void main(String[] args) throws Exception {
        new StaticRef();
    }
}

The justification I've heard for this approach is that, for re-use, StaticRef can be easily modified to act as a Java Bean by doing little more than removing the main method. Please do comment. See also this answer, for a similar solution, or this other answer.

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