static이란?


void main() {
  
  
  Employee seulgi = Employee('슬기');
  Employee chorong = Employee('초롱');
  
  seulgi.name = '코드팩토리';
  seulgi.printNameAndBuilding(); // 같은 클래스에서 인스턴스를 만들었으나
  chorong.printNameAndBuilding(); // 각 인스턴스의 값을 가져오면 변수마다 다름 -> 인스턴스 귀속
  
  Employee.building = '오투타워'; // static 변수에 값 할당
  seulgi.printNameAndBuilding(); // building 값 모든 인스턴스에서 동일
  chorong.printNameAndBuilding(); // building 값 모든 인스턴스에서 동일 -> 클래스 귀속
  
  Employee.printBuilding(); // static method
}

class Employee {
  // 알바생이 일하고 있는 건물
  static String? building;
  //알바생 이름
  String name;
  
  Employee(
    this.name,
  );
  
  void printNameAndBuilding() {
    print('제 이름은 $name입니다. $building 건물에서 근무하고 있습니다.');
  }
  
  static void printBuilding() {
    print('저희는 $building 건물에서 근무중입니다.');
  }
  
}
//실행결과
제 이름은 코드팩토리입니다. null 건물에서 근무하고 있습니다.
제 이름은 초롱입니다. null 건물에서 근무하고 있습니다.
제 이름은 코드팩토리입니다. 오투타워 건물에서 근무하고 있습니다.
제 이름은 초롱입니다. 오투타워 건물에서 근무하고 있습니다.
저희는 오투타워 건물에서 근무중입니다.