18

As a rule in object-oriented paradigm, a static method can have access only to static variables and static methods. If it is so, an obvious question arises as to how can the main() method in Java has access to non-static members (variables or methods) even though it is specifically public static void...!!!

Bhavesh
  • 303
  • 2
  • 3
  • 9
  • How did you come to the conclusion that the main method has access to instance variables and methods? – Laf Oct 25 '11 at 19:34

7 Answers7

30

The main method does not have access to non-static members either.

public class Snippet
{
   private String instanceVariable;
   private static String staticVariable;

   public String instanceMethod()
   {
      return "instance";
   }

   public static String staticMethod()
   {
      return "static";
   }

   public static void main(String[] args)
   {
      System.out.println(staticVariable); // ok
      System.out.println(Snippet.staticMethod()); // ok

      System.out.println(new Snippet().instanceMethod()); // ok
      System.out.println(new Snippet().instanceVariable); // ok

      System.out.println(Snippet.instanceMethod()); // wrong
      System.out.println(instanceVariable);         // wrong 
   }
}
tjg184
  • 4,508
  • 1
  • 27
  • 54
  • But any method whether static or NOT does not have access to non-static members [if you do it like in the exmple above as `Snippet.instanceMethod()` and not by instantiating an object: `new Snippet().instanceMethod()`]. So why all the fuss about that static method has no access to non-static members? – Haider Apr 03 '21 at 08:10
14

By creating an object of that class.

public class Test {
    int x;

    public static void main(String[] args) {
        Test t = new Test();
        t.x = 5;
    }
}
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
5

The main() method cannot have access to the non-static variables and methods, you will get “non-static method cannot be referenced from a static context” when you try to do so.

This is because by default when you call/access a method or variable it is really accessing the this.method() or this.variable. But in the main() method or any other static method(), no "this" objects has yet been created.

In this sense, the static method is not a part of the object instance of the class that contains it. This is the idea behind utility classes.

To call any non-static method or variable in a static context, you need to first construct the object with a constructor or a factory like your would anywhere outside of the class.

Desmond Zhou
  • 1,369
  • 1
  • 11
  • 18
  • Hi Desmond will it be possible for you to give an example for your answer? – joekevinrayan96 Jan 24 '21 at 17:25
  • @JoeKevinRayan Here you go.. public class MyClass { int x = 10; public static void main(String args[]) { System.out.println("x" + x); //Not allowed System.out.println("x-" + new MyClass().x); //Allowed } } – Zoran777 Sep 09 '21 at 17:22
2
YourClass inst = new YourClass();
inst.nonStaticMethod();
inst.nonStaticField = 5;
xthexder
  • 1,555
  • 10
  • 22
2

You have to instantiate the object you wish to access.

For example

public class MyClass{

  private String myData = "data";

  public String getData(){
     return myData;
  }

  public static void main(String[] args){
     MyClass obj = new MyClass();
     System.out.println(obj.getData());
  }
}
Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
1

Through the reference to the object.

public class A {
    private String field;


    public static void main(String... args) {
        //System.out.println(field); <-- can not do this
        A a = new A();
        System.out.println(a.field); //<-- access instance var field that belongs to the instance a 
    }
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

You must create an instance of the class in order to reference instance variables & methods.

Rudra Saraswat
  • 71
  • 1
  • 13
dbreaux
  • 4,982
  • 1
  • 25
  • 64