Why?

Java - clazz 변수명은 왜 사용할까?

Cold Bean 2024. 11. 1. 15:26
728x90

개요

Java 코드를 살펴보다 보면 간혹 Class 객체 변수명으로 clazz를 사용하는 것을 볼 수 있다. 오늘도 Mockito 동작 원리가 궁금해서 관련 코드를 살펴보던 중에 또 clazz 변수명을 보게 되었다. 

이전에 clazz를 보면 '왜 이름을 저렇게 지었지?'라고 생각만 하고 넘어갔지만 이번에는 조금 더 호기심이 생겨 clazz 변수명을 사용한 이유에 대해 알아보았다.

public class MockSettingsImpl<T> extends CreationSettings<T> implements MockSettings, MockCreationSettings<T> {
    ...
    private boolean invocationListenersContainsType(Class<?> clazz) {
        Iterator var2 = this.invocationListeners.iterator();

        InvocationListener listener;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            listener = (InvocationListener)var2.next();
        } while(!listener.getClass().equals(clazz));

        return true;
    }
    ...
}

 

clazz를 변수명으로 사용하는 이유

이유는 간단하다. Java에서 class는 예약어이기 때문이다. 그래서 s와 발음이 비슷한 z로 변경하여 clazz를 사용하게 되었다고 한다.

JDK 1.0부터 clazz 변수명을 사용했다고 한다. 

// class는 Class를 정의하는데 사용되는 예약어다.
public class Application {
    String.class.getName();	
}

컴파일 에러가 발생한다.

 

stackoverflow를 살펴보면 나처럼 clazz 변수명 사용 이유에 대해 궁금해하는 사람들이 있었고 댓글에는 clazz 변수명이 적절한 선택인지에 대해 의견을 나누는 걸 볼 수 있다. 내가 clazz라는 변수명을 주제로 글까지 쓰게 된걸 보면 좀 더 나은 변수명을 선택하는 방법이 있지 않았을까 싶기도 하다. 

 

참조

https://stackoverflow.com/questions/2529974/why-do-java-programmers-like-to-name-a-variable-clazz

 

Why do Java programmers like to name a variable "clazz"?

I've seen lots of code with declarations like Class clazz. Where does this originate from? Is this some kind of convention? I think ‘clazz’ is not even an English word, has no meaning at all, how c...

stackoverflow.com

https://dpericich.medium.com/what-is-the-difference-between-class-klass-and-clazz-416b7d2736c1

 

What is the Difference Between Class, Klass, and Clazz?

Why am I seeing clazz or klass instead of “class” in my code? What are the differences betweeen these different spellings?

dpericich.medium.com

 

728x90