1

Okay, so don't ask why, but I'm trying to make a universal public static void main() method. I've already tried to use these two methods;

public class Foo {
    public static void main(String[] args){
        try {
            this.getClass().newInstance().check();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void check() {
        System.out.println("Check succesful");
    }
}

The error I get is that this "Cannot be used in a static context"
Okay, so I know I can't use this in a static context, but what I want to know is how I can replace it, without using Foo.check()

If possible, how should I do this? If not, I'd like to know why.

Charlie
  • 978
  • 1
  • 7
  • 27

5 Answers5

3

Looking at How to call getClass() from a static method in Java? and Getting the class name from a static method in Java something like

interface Checkable {
    public void check();
}

public class Foo implements Checkable {
    public static void main(String[] args){
        try {
            Class currentClass = new Object() { }.getClass().getEnclosingClass();
            Checkable instance = (Checkable) currentClass.newInstance();
            instance.check();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void check() {
        System.out.println("Check succesful");
    }
}

might do the trick, but I'm not sure I should recommend doing that...

Community
  • 1
  • 1
godfatherofpolka
  • 1,645
  • 1
  • 11
  • 24
2

this is the current instance. You don't have an instance in a static method. Please see I want to know the difference between static method and non-static method

Do this instead:

public class Foo {
  public static void main(String[] args) {
    new Foo().check();
  }

  public void check() {
    System.out.println("Check succesful");
  }
}

As an answer to the comment (I don't seem to be able to make comments yet): No. The only other way is to make check() static as well and call Foo.check(), but you didn't want to do that.

Community
  • 1
  • 1
Roger Gustavsson
  • 1,689
  • 10
  • 20
1

There's no this in a static context; that's exactly what static means. The approach you're trying will not work. You could perhaps supply the name of the class you're interested in on the command line.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

As, I know in java instance variable cannot be accessed in static block util you don't have object of that class for which class you want to access instance variable and this in java is a instance variable. so you cannot access this variable in any static block or you required object reference for this.for further clarification check here.

Manoj Sharma
  • 596
  • 1
  • 6
  • 23
0

why dont you try this ?

  public class Foo
 {
    public static void main(String[] args){
        try 
        {
            Foo obj = new Foo();
            //  this.getClass().newInstance().check();
             obj.check();
        }
        catch(Exception e) 
        {
            e.printStackTrace();
        }
    }

    public void check() 
    {
        System.out.println("Check succesful");
    }
}
Wild Eagle
  • 41
  • 1
  • 4