public static void main () access non static variable

Viewed 49500

It is said that non-static variables cannot be used in a static method. But public static void main does. How is that?

6 Answers
public class XYZ
{
   int i=0;
   public static void increament()
       {
       i++;   
       }
}
public class M
{
    public static void main(String[] args)
    {
    XYZ o1=new XYZ();
    XYZ o2=new XYZ();
    o1.increament(); 
    XYZ.increament(); //system wont be able to know i belongs to which object 
                  //as its increament method(static method)can be called using   class name system 
                  //will be confused changes belongs to which object.
    }
}
Related