集册 Java实例教程 如何访问(读/写)类变量和类的实例变量

如何访问(读/写)类变量和类的实例变量

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

563
如何访问(读/写)类变量和类的实例变量

class Human { 

    String name;        // An instance variable 

    String gender;      // An instance variable 
    /* from 
    时 代 J a v a 公 众 号*/

    static long count;  // A class variable because of the static modifier 

} 


public class Main {  

  public static void main(String[] args) {

    Human jack = new Human();

    // Increase count by one

    Human.count++;

  

    // Assign values to name and gender/* 来自 时 代 J     a    v  a - nowjava.com*/

    jack.name = "Ana";

    jack.gender = "Female";

  

    // Read and print the values of name, gender and count

    String jackName = jack.name;

    String jackGender = jack.gender;

    long population = Human.count;

  

    System.out.println("Name: " + jackName);

    System.out.println("Gender: " + jackGender);

    System.out.println("Population: " + population);

  

    // Change the name
展开阅读全文