为什么Java程序员喜欢命名变量“clazz”?

时间:2021-08-28 13:23:00

I've seen lots of codes have declaration 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 can so many programmers name a wrong name coincidentally ?

我已经看到很多代码都有类似Clazz的声明,这是从哪里来的?这是某种惯例吗?我认为'clazz'甚至不是英文单词,根本没有任何意义,这么多程序员怎么能巧妙地命名一个错误的名字呢?

9 个解决方案

#1


134  

clazz has been used in Java in place of the reserved word "class" since JDK 1.0. "class" is what you want, but abbreviating or inserting junk ("a", "the", "_", etc) reduces clarity. clazz just says class. "International" English speakers (those reading both English and American) are used to transposing 's' and 'z'.

自JDK 1.0起,c​​lazz已在Java中用于代替保留字“class”。 “class”是你想要的,但缩写或插入垃圾(“a”,“the”,“_”等)会降低清晰度。 clazz只是说课。 “国际”英语使用者(英语和美语阅读者)用于转置's'和'z'。

Since Java has had disclosed source and a suitable culture right from the start, worthwhile Java code and tutorials pick up the same conventions. That's one of the great things about the Java ecosystem, which I think has been an important part of its success.

由于Java从一开始就已经公开了源代码和合适的文化,因此有价值的Java代码和教程会采用相同的约定。这是Java生态系统的一大优势,我认为这是其成功的重要组成部分。

#2


61  

Because they cannot use the word they want to use which is 'class'. It is reserved.

因为他们不能使用他们想要使用的单词“class”。这是保留的。

#3


12  

It's simply because 'class' is a reserved keyword, hence Class class isn't allowed. Therefore you'll see Class clazz or Class cls.

这只是因为'class'是保留关键字,因此不允许使用Class类。因此,您将看到Class clazz或Class cls。

#4


7  

It comes down to the actual compiler and its ability to distinguish what a token means within its context. However, in this particular case, it is the compiler's inability to distinguish what the token class means in a different context. It is a hard and fast rule that class, regardless of its context, is used to denote the declaration of a class, and as such it is a reserved word. That is as simple and as low-level as it gets.

它归结为实际的编译器及其区分令牌在其上下文中的含义的能力。但是,在这种特殊情况下,编译器无法区分令牌类在不同上下文中的含义。这是一个硬性规则,类,无论其上下文如何,都用于表示类的声明,因此它是一个保留字。这就像它获得的那样简单和低级。

If you feel compelled, you could write your own Java compiler to include a contextual rule that will allow you to use class as a variable name. Though I think it would be far better use of your time to just use clazz or klass -- it would probably be good for your health as well.

如果您感到有必要,可以编写自己的Java编译器以包含允许您将类用作变量名的上下文规则。虽然我认为使用clazz或klass会更好地利用你的时间 - 这对你的健康也可能有好处。

#5


4  

where does this originate from ?

这起源于哪里?

I saw it first at Josh Bloch's puzzlers. But I'm pretty sure it was used much earlier by other developers. Josh Bloch just made it more famous.

我首先在Josh Bloch的益智游戏中看到了它。但我很确定其它开发人员早就使用过它。乔什布洛赫让它更有名。

#6


4  

Java does not have a feature that allows you to use a keyword as an identifier, unlike C# with its @ prefix (e.g. @class is a valid identifier.)

Java没有允许您使用关键字作为标识符的功能,与C#及其@前缀不同(例如@class是有效的标识符。)

#7


2  

Declaration Class clazz is popular in Java world, but it may be awkward for newcomers and spellcheckers. I've heard some people saying that it should be avoided according to principle of least astonishment.

Declaration Class clazz在Java世界中很受欢迎,但对于新手和拼写者来说可能会很尴尬。我听说有些人说应该根据最不惊讶的原则来避免。

As it is possible to say that a Class object represents a type, I personally prefer to declare such variables as Class type.

由于可以说Class对象表示一个类型,我个人更喜欢将这些变量声明为Class类型。

#8


1  

It is just a English word replaced(Equavalent) by Keyword Class Keyword, to make people understand that it is a Class. and it is almost to increase the readability of the Code

它只是一个英文单词被Keyword Class Keyword取代(Equavalent),让人们明白它是一个类。这几乎是为了增加守则的可读性

Nothing big Logic involved in this

没有什么大逻辑涉及到这一点

#9


0  

We use clazz because class is a type header. For example, you use class here:

我们使用clazz因为class是一个类型标题。例如,您在此处使用class:

public class HelloWorld {

and here:

和这里:

Object.class.getName();

So unfortunately, we have to use alternate names such as clazz.

不幸的是,我们必须使用诸如clazz之类的替代名称。

#1


134  

clazz has been used in Java in place of the reserved word "class" since JDK 1.0. "class" is what you want, but abbreviating or inserting junk ("a", "the", "_", etc) reduces clarity. clazz just says class. "International" English speakers (those reading both English and American) are used to transposing 's' and 'z'.

自JDK 1.0起,c​​lazz已在Java中用于代替保留字“class”。 “class”是你想要的,但缩写或插入垃圾(“a”,“the”,“_”等)会降低清晰度。 clazz只是说课。 “国际”英语使用者(英语和美语阅读者)用于转置's'和'z'。

Since Java has had disclosed source and a suitable culture right from the start, worthwhile Java code and tutorials pick up the same conventions. That's one of the great things about the Java ecosystem, which I think has been an important part of its success.

由于Java从一开始就已经公开了源代码和合适的文化,因此有价值的Java代码和教程会采用相同的约定。这是Java生态系统的一大优势,我认为这是其成功的重要组成部分。

#2


61  

Because they cannot use the word they want to use which is 'class'. It is reserved.

因为他们不能使用他们想要使用的单词“class”。这是保留的。

#3


12  

It's simply because 'class' is a reserved keyword, hence Class class isn't allowed. Therefore you'll see Class clazz or Class cls.

这只是因为'class'是保留关键字,因此不允许使用Class类。因此,您将看到Class clazz或Class cls。

#4


7  

It comes down to the actual compiler and its ability to distinguish what a token means within its context. However, in this particular case, it is the compiler's inability to distinguish what the token class means in a different context. It is a hard and fast rule that class, regardless of its context, is used to denote the declaration of a class, and as such it is a reserved word. That is as simple and as low-level as it gets.

它归结为实际的编译器及其区分令牌在其上下文中的含义的能力。但是,在这种特殊情况下,编译器无法区分令牌类在不同上下文中的含义。这是一个硬性规则,类,无论其上下文如何,都用于表示类的声明,因此它是一个保留字。这就像它获得的那样简单和低级。

If you feel compelled, you could write your own Java compiler to include a contextual rule that will allow you to use class as a variable name. Though I think it would be far better use of your time to just use clazz or klass -- it would probably be good for your health as well.

如果您感到有必要,可以编写自己的Java编译器以包含允许您将类用作变量名的上下文规则。虽然我认为使用clazz或klass会更好地利用你的时间 - 这对你的健康也可能有好处。

#5


4  

where does this originate from ?

这起源于哪里?

I saw it first at Josh Bloch's puzzlers. But I'm pretty sure it was used much earlier by other developers. Josh Bloch just made it more famous.

我首先在Josh Bloch的益智游戏中看到了它。但我很确定其它开发人员早就使用过它。乔什布洛赫让它更有名。

#6


4  

Java does not have a feature that allows you to use a keyword as an identifier, unlike C# with its @ prefix (e.g. @class is a valid identifier.)

Java没有允许您使用关键字作为标识符的功能,与C#及其@前缀不同(例如@class是有效的标识符。)

#7


2  

Declaration Class clazz is popular in Java world, but it may be awkward for newcomers and spellcheckers. I've heard some people saying that it should be avoided according to principle of least astonishment.

Declaration Class clazz在Java世界中很受欢迎,但对于新手和拼写者来说可能会很尴尬。我听说有些人说应该根据最不惊讶的原则来避免。

As it is possible to say that a Class object represents a type, I personally prefer to declare such variables as Class type.

由于可以说Class对象表示一个类型,我个人更喜欢将这些变量声明为Class类型。

#8


1  

It is just a English word replaced(Equavalent) by Keyword Class Keyword, to make people understand that it is a Class. and it is almost to increase the readability of the Code

它只是一个英文单词被Keyword Class Keyword取代(Equavalent),让人们明白它是一个类。这几乎是为了增加守则的可读性

Nothing big Logic involved in this

没有什么大逻辑涉及到这一点

#9


0  

We use clazz because class is a type header. For example, you use class here:

我们使用clazz因为class是一个类型标题。例如,您在此处使用class:

public class HelloWorld {

and here:

和这里:

Object.class.getName();

So unfortunately, we have to use alternate names such as clazz.

不幸的是,我们必须使用诸如clazz之类的替代名称。