Static methods use non-static data

Static method only allows access to static data, then, how in a static method access instance members of the class (ie no additional field or method static keyword)?

You can not use non-static data or a non-static method in a static method. In fact, nor is it can not be used, can not be used directly. We can instantiate the class to do this.

package ppt_test;

/ * Static method only allows access to static data, then instance members, how to access the class in a static method
(Ie no additional field or method static keywords)? * /

public class Test3 {
	public int a1=1;
	static public int a2=2;
	public static void f1()
	{
		System.out.println ( "static method call");
	}
	public void f2()
	{
		System.out.println ( "non-static method call");
	}
    public static void main(String args[])
    {
    	Test3 a=new Test3();
    	System.out.println(a.a1);
    	a.f1 ();
    	System.out.println(a2);
    	a.f2();
    }
}

  

       

 

 

  

Guess you like

Origin www.cnblogs.com/xp-thebest/p/11704076.html