197

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?

11
  • 57
    don't forget klass! Commented Mar 27, 2010 at 16:38
  • 37
    @Peter: good point, that's a klassic. Commented Mar 27, 2010 at 21:11
  • 19
    Aleksey Shipilev has proposed using сlass spelled with a Cyrillic с as an alternative. Commented Jan 24, 2016 at 4:07
  • 17
    @StuartMarks I am rejecting that proposition. You're welcome. Commented Aug 2, 2016 at 15:05
  • 25
    In England, we normally use 'clarse'. Commented May 19, 2017 at 17:16

9 Answers 9

220

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It's hard to agree that "clazz" is more clear than "_class" or "myClass". If I'd seen "_class" in code, the intent would have been more obvious than a non-word that sent me to Google (and to this page).
@uscjeremy: Personally, I prefer the use of underscores to having identifiers which differ in case only. Were I designing a language, I would have it be a hybrid of case-sensitive and non-case-sensitive rules: any identifier could only be used in exactly the original casing, but declaration of any identifier would hide any pre-existing identifiers which differed only in casing. Having Foo defined at class scope and foo at local-variable scope should be legal, but all references to the class-scope variable within the scope of the local should require this.Foo. Such a rule would...
@supercat The Unicode General Category of Punctuation is what I was fishing for. unicode.org/faq/punctuation_symbols.html / I wouldn't design C#. In Java, you get away with a method called fnord and a field called fnord without any issues, capitalisation or nonsense words. I think that will suffice as a counter example.
@uscjeremy It's hard to agree that "clazz" is more clear than "_class" or "myClass" — True. However, using "_class" would not be an option, because it clearly conflicts with the Java naming conventions.
imho _class is confusing because some annotation processors generate classes named Foobar_.java.
|
91

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

7 Comments

Yeah, I know , but I think spell a variable right would be more meaningful.
@Tony - how can you say "you know" and demand that the variable name be spelled right in the same comment? They are mutually exclusive.
@Tony, you simply can't name a variable "class", it's a reserved word.
@duffymo : It's funny .I know class is reserved word , and I indeed demand the variable name be spelled right , why are they mutually exclusive ?? do you have to call it 'class' ?
I am guessing Tony wants the variable to be named something more descriptive than just "class". For example, "x" could be said to be spelled properly, but it does not mean as much as "numberOfComments"
|
21

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

12 Comments

I can't think of a reason to name a class Class since there are so many classes in any given project that calling a class THE class seems kind of pointless. I also can't think of a reason to call a VARIABLE class...
You did not name a class Class , it's java API.
@Blindy: What about java.lang.Class? You don't have much choice there.
@Blindy: There already is a class called Class, which refers to, well, an object's class. The perfect reason to have a variable called 'class' is when you're working with an object's class (ie: figuring out what type an object is)
I would consider informative ClassInformation, or ClassMetadata or something similar. Every class is a class, but you don't name them all Class, you name them by their main function.
|
9

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.

1 Comment

A word about inability: Java was designed as a reserved-words language. Non-reserved-words languages can successfully compile and syntax-highlight statements such as if if then then else else, despite (un)readability. So the compiler was purposely left unable
8

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.

Comments

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.

Comments

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

2 Comments

That's a different identifier. class and $class are both valid (but different) identifiers in the JVM. There's no identifier you can write in a Java source file that translates to "class" in the VM.
Or r# in Rust.
3

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

1 Comment

I think it's because z is just s flipped
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.