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

JavaNaming Conventions

Java Problem Overview


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 can so many programmers name a wrong name coincidentally?

Java Solutions


Solution 1 - Java

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 British and American English) are used to transposing 's' and '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.

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

> 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.

Solution 6 - Java

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.)

Solution 7 - Java

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.

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

Solution 8 - Java

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

Nothing big Logic involved in this

Solution 9 - Java

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

public class HelloWorld {

and here:

Object.class.getName();

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSawyerView Question on Stackoverflow
Solution 1 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 2 - JavaPresident James K. PolkView Answer on Stackoverflow
Solution 3 - JavasfusseneggerView Answer on Stackoverflow
Solution 4 - JavaJohnView Answer on Stackoverflow
Solution 5 - JavaRomanView Answer on Stackoverflow
Solution 6 - JavafinnwView Answer on Stackoverflow
Solution 7 - JavaMichal KordasView Answer on Stackoverflow
Solution 8 - JavagmhkView Answer on Stackoverflow
Solution 9 - JavaRomejanicView Answer on Stackoverflow