Static and non-static(instance) methods

2023. 11. 12. 15:54·개발

1. Definition and Access

static instance
Belog to the class itself, not to instances of the class. 

e.g) call them on the class directly
-> Classname.staticMethod()
Belog to instances of the class. Need to create an object of the class to use these methods.

e.g ) const obj = new ClassName();
obj.instanceMethod()

 

2. Use of 'this' keyword

static instance
Cannot use 'this' to refer to an instance of the class, as they do not have access to data stored in specific objects unless passed exlicitly.

Can use 'this' to refer to the instance of the class they belog to, allowing them to access other instance methods and properties of that particular object.

 

3. Purpose

static instance
Used for utility functions that don't require the objct's state(i.e, properties). They can't access instance properties methods directly and are often used for operations that apply to the entire class.(factory methods) Used to operate on the data of specific objects. They can access and modify the state of individual instances.

 

more about factory methods?

- Factory method is a creational design patter that provides an interface for creating objects in a superclass, but allows subclasses to alter th type of objects that will be created.

 

4. Memory allocation

static instance
Since they belong to the class, they are loaded into memory once, at the start of the execution. They are not duplicated across instances.

A new copy of each method is created in meomory for every instance of the class.

 

저작자표시 비영리 (새창열림)

'개발' 카테고리의 다른 글

Generic Programming and Type Parameter  (1) 2023.11.12
Typescript Type narrowing  (0) 2023.11.12
NestJs - Mircoservices  (2) 2023.11.08
How to do a code review  (0) 2023.11.03
Error handling - nestJs + GraphQL  (1) 2023.07.17
'개발' 카테고리의 다른 글
  • Generic Programming and Type Parameter
  • Typescript Type narrowing
  • NestJs - Mircoservices
  • How to do a code review
senyalog
senyalog
개발 블로그 https://github.com/iamyunjuda
  • senyalog
    Senya의 개발 블로그
    senyalog
  • 전체
    오늘
    어제
    • 분류 전체보기 (77)
      • 일상 (6)
      • 알고리즘 이론 (14)
        • 백준 (8)
      • 개발 (47)
        • Server (7)
        • 인턴 (11)
        • Javascript (0)
      • 경제 (4)
  • 블로그 메뉴

    • Github
    • 홈
  • 링크

    • Github
  • 공지사항

  • 인기 글

  • 태그

    대학생인턴
    인턴
    백엔드
    깃
    백준문제
    TypeScript
    이마고웍스
    공부
    알고리즘공부
    백준풀이
    공대생
    개발공부
    코딩
    인턴생활
    생각
    경제
    GIT
    알고리즘
    서버
    코딩테스트
    MSA
    개발자
    컴공
    백준
    대학생
    개발
    C++
    카페
    일상
    nestjs
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
senyalog
Static and non-static(instance) methods
상단으로

티스토리툴바