集册 Java实例教程 从静态和非访问类字段

从静态和非访问类字段

欢马劈雪     最近更新时间:2020-01-02 10:19:05

370
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从静态和非静态方法访问类字段

public class Main {

  public static void main(String[] args) {//N o w J a v a . c o m - 时  代  Java

    MyClass mt = new MyClass();

    System.out.println("Invoking instance method...");

    mt.printMN();

    System.out.println("Invoking class method on class name...");

    MyClass.printM();


    System.out.println("Invoking class method on an instance...");


    // Invoke the class method using the instance reference

    mt.printM();

  }

}


class MyClass {

  static int m = 100; // A static variable

  int n = 200; // An instance variable


  static void printM() {

    System.out.println("printM() - m = " + m);
    /**
     from
    * nowjava.com - 时  代  Java 
    **/

  }


  void printMN() {

    
展开阅读全文