Skip to content

Keywords

  • Class
  • Modifier
  • Static

Class

A class is a container that contains the block of code that includes fields, methods, constructors, getters and setters.

package JavaSE.keywords.classExample;

/**
 * @Author: eve
 * @Date: 2025-01-31 13:11
 */

/*

A class is a container that contains the block of code that includes fields,
methods, constructors, getters and setters.
 */
class Person {
    private String name; // field

    // 为什么name 不用static关键字?
    // 如果name用static关键字,就是静态字段
    // 静态字段是属于类的,不属于对象的
    // 如果name用static关键字,就不能用this关键字访问
    // 比如下面这样写是错误的
    // private static String name;
    // public Person(String name){
    //     this.name = name;
    // }

    public Person(String name){ // constructor
        this.name = name; // this keyword is used to refer to the current object
    }

    public void hi(){ // method
        System.out.println("Hi, I am " + name);
    }

    // getter
    public String getName(){
        return name;
    }

    // setter
    public void setName(String name){
        this.name = name;
    }
}

// how to create an object of a class
// Person p1 = new Person("Mark");

// class 是否要加public关键字取决于是否在同一个package下
// 如果在同一个package下,可以不加public关键字
// 如果在不同的package下,需要加public关键字
// private关键字表示只有在同一个类中才能访问
// 如果想在其他类中访问,需要使用public关键字
// private关键字的作用是保护数据
// 如果不想让其他类访问,可以使用private关键字
// 比如此例中,name字段是private的,只能在Person类中访问

// 什么叫只能在Person类中访问?
// 比如在Main类中,如果想访问Person类中的name字段,是无法访问的
// 因为name字段是private的,只能在Person类中访问
// p1.name = "Tom"; // 无法访问
// System.out.println(p1.name); // 无法访问
// 但是可以通过getName()方法访问


// Person中 method 为什么不用static关键字?
// 如果method用static关键字,就是静态方法
// 静态方法是属于类的,不属于对象的
// 如果method用static关键字,就不能访问name字段
// 比如下面这样写是错误的
// public static void hi(){
//     System.out.println("Hi, I am " + name);
// }
// 因为name字段是属于对象的,而不是属于类的
// 所以method不用static关键字

// 属于对象和属于类的区别
// 属于对象的字段,每个对象都有一份
// 属于类的字段,只有一份,所有对象共享
// 属于对象的方法,每个对象都有一份
// 属于类的方法,只有一份,所有对象共享
// 属于对象的字段和方法,用this关键字访问

// 为什么name 不用static关键字?
// 如果name用static关键字,就是静态字段
// 静态字段是属于类的,不属于对象的
// 如果name用static关键字,就不能用this关键字访问
// 比如下面这样写是错误的
// private static String name;
// public Person(String name){
//     this.name = name;
// }

// 为什么static name 不能用this关键字访问?
// 因为this关键字是用来访问属于对象的字段和方法的
// 如果name用static关键字,就是属于类的字段
// 所以不能用this关键字访问

// 如何判断一个字段或方法是属于对象还是属于类的?
// 如果字段或方法用static关键字,就是属于类的
// 如果字段或方法不用static关键字,就是属于对象的
package JavaSE.keywords.classExample;

/**
 * @Author: eve
 * @Date: 2025-01-31 13:13
 */
public class Main {
    public static void main(String[] args) {
        Person p1 = new Person("Mark");
        p1.hi();

        // name is private, cannot be accessed
//         p1.name = "Tom"; // 无法访问
//        System.out.println(p1.name); // 无法访问
    }
}
Hi, I am Mark

Process finished with exit code 0

MODIFIER

  • Access Modifier

Specify accessibility of field, method, constructor & class

  • Non-Access Modifier

e.g. static, abstract, final

Access Modifier

Determine access rights for the class and its members

  • public
  • private
  • protected
  • default

ACCESS MODIFIER SCOPE

Access Modifier Class or member can be referenced by...
public methods of the same class, and methods of other classes
private methods of the same class only
protected methods of the same class, methods of subclasses, and methods of classes in the same package
No access modifier (package access) methods in the same package only

Non-Access Modifier

static

final

abstract

static vs non-static

static

  • Class
  • compile time or early binding

Non-Static

  • Object
  • Runtime time or dynamic binding
CLASS VARIABLE vs INSTANCE VARIABLE

What is the difference between following statements?

public int x;

public static int x;

package JavaSE.staticExample;

public class VariableDemo {
    static int staticVariable = 0;
    int instanceVariable = 0;

    public static void main(String[] args){
        System.out.println("original staticVariable: "+ staticVariable);

        // instance variable can only be accessed through Object reference.
        //System.out.println(instanceVarible);

        VariableDemo object1 = new VariableDemo();
        System.out.println("object1.instanceVariable original: " + object1.instanceVariable); // object reference

        object1.staticVariable = 1;
        object1.instanceVariable = 1;
        System.out.println("object1.instanceVariable: " + object1.instanceVariable); // object reference
        System.out.println("object1.staticVariable: " + object1.staticVariable); // object reference

        VariableDemo object2 = new VariableDemo();
        // each object has its own copy instance variable
        System.out.println("object2.instanceVariable: " + object2.instanceVariable); // object reference

        // common to all object of a class
        System.out.println("object2.staticVariable: " + object2.staticVariable); // object reference

    }
}
original staticVariable: 0
object1.instanceVariable original: 0
object1.instanceVariable: 1
object1.staticVariable: 1
object2.instanceVariable: 0
object2.staticVariable: 1

Process finished with exit code 0
Class Variables Instance Variable
Class variables are declared with keyword static. Instance variables are declared without static keyword.
Class variables are common to all instances of a class. These variables are shared between the objects of a class. Instance variables are not shared between the objects of a class. Each instance will have their own copy of instance variables.
As class variables are common to all objects of a class, changes made to these variables through one object will reflect in another. As each object will have its own copy of instance variables, changes made to these variables through one object will not reflect in another object.
Class variables can be accessed using either class name or object reference. Instance variables can be accessed only through object reference.

STATIC METHOD vs NON-STATIC METHOD

What is the difference between following statements?

public int getX();

public static int getX();

static Method non-static Method
Access instance variables? no yes
Access static class variables? yes yes
Call static class methods? yes yes
Call non-static instance methods? no yes
Use the object reference this? no yes