188

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?

user3840170
  • 26,597
  • 4
  • 30
  • 62
Sawyer
  • 15,581
  • 27
  • 88
  • 124

9 Answers9

213

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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • 119
    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 Jul 10 '13 at 20:13
  • @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 Jan 18 '14 at 17:04
  • @uscjeremy: ...in my opinion combine the advantages of case-sensitive and non-case sensitive languages. Case-insensitive languages have the benefit that phonetic readings of an identifier name "foo" will be unambiguous without having to say "uppercase foo" or "lowercase foo", but there's no real reason to allow an identifier to be spelled `someThing` in some places and `sOmEtHiNg` in others. – supercat Jan 18 '14 at 17:15
  • @supercat Were I designing a language, underscores would be symbols as they are in Unicode. Types vs variables/functions would be determined by language rules about case. Even in cases of reading code out loud, context is sufficient for humans. / Anyway, this is why it was done. If you need to google what `clazz` means for in **`Class clazz`**, then I don't see why shouldn't need to google the meaning of introducing a spurious, meaningless symbol or word. – Tom Hawtin - tackline Jan 18 '14 at 17:26
  • @TomHawtin-tackline: What do you mean by "symbols"? As for context being sufficient, if a class has a property `Fnord` with backing field `fnord` (common C# practice), how would one read aloud the code within the class to make clear which accesses are done directly to the backing field and which are to the property? If the names were `Fnord` and `_fnord` (a common vb.net practice), the need for different pronunciation would more obvious. In Java, the property would be a pair of `getFnord`/`setFnord` methods, though some might argue the `get`/`set` prefixes shouldn't be necessary. – supercat Jan 18 '14 at 18:24
  • 1
    @supercat The Unicode General Category of Punctuation is what I was fishing for. http://www.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. – Tom Hawtin - tackline Jan 18 '14 at 21:12
  • 6
    @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](http://www.oracle.com/technetwork/java/codeconventions-135099.html). – MC Emperor Jan 24 '17 at 09:49
  • imho `_class` is confusing because some annotation processors generate classes named `Foobar_.java`. – Hannes Schneidermayer Jun 05 '18 at 12:55
86

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

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • 3
    Yeah, I know , but I think spell a variable right would be more meaningful. – Sawyer Mar 27 '10 at 16:37
  • 25
    @Tony - how can you say "you know" and demand that the variable name be spelled right in the same comment? They are mutually exclusive. – duffymo Mar 27 '10 at 17:38
  • 3
    @Tony, you simply can't name a variable "class", it's a reserved word. – Steve Kuo Mar 27 '10 at 17:57
  • 1
    Class isn't reserved, only class. – Malfist Mar 27 '10 at 18:05
  • 8
    @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' ? – Sawyer Mar 28 '10 at 04:09
  • 2
    If "class" is the right spelling, then you can't use it. So they are mutually exclusive. Do you really not see it? Then you don't know. – duffymo Mar 28 '10 at 13:22
  • 16
    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" – Peter Recore Mar 28 '10 at 14:58
20

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

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • 8
    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`... – Blindy Mar 27 '10 at 16:25
  • 8
    You did not name a class Class , it's java API. – Sawyer Mar 27 '10 at 16:28
  • 3
    @Blindy: What about java.lang.Class? You don't have much choice there. – Dan Dyer Mar 27 '10 at 16:28
  • 15
    @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) – Reverend Gonzo Mar 27 '10 at 16:29
  • Because sometimes you're writing code that has to manipulate actual classes. If the variable you're working with is, in fact, a class, but you can't call it "class", then you come up with something similar enough that your meaning is clear. – jwismar Mar 27 '10 at 16:30
  • 1
    @Dan, I would call that `ClassInformation`, or something like C#'s `TypeInfo`. You're neither defining a class not instantiating a class when you use that.. class. You're working with reflection to access a class' information. @Tony, Just because it's the java API doesn't make it right. – Blindy Mar 27 '10 at 16:34
  • @Blindy: with all the problems in the Java API, I would not consider that a major one (or even one at all) – BlueRaja - Danny Pflughoeft Mar 27 '10 at 16:40
  • Fair enough, I'm just saying this looks incredibly weird and uninformative (not to mention inaccurate). – Blindy Mar 27 '10 at 16:44
  • @Tony, you can certainly have your own Class class, as long as it is in its own package. You'll have to use the fully resolved class name to distinguish it from java.lang.Class - and endure the eternal scorn from users of your code for choosing such an uninformative name. – duffymo Mar 27 '10 at 17:37
  • @Blindy What about the simple case `Class> cls = object.getClass();` You could certainly call the variable `objectClass` but that's not much more informative than `cls`. Or think about a method `List> getProperties(Class> cls)`. So what is so weird about it? What would you consider informative (not to mention accurate)? – sfussenegger Mar 28 '10 at 08:03
  • 3
    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. – Blindy Mar 28 '10 at 08:40
  • 2
    @Blindy as a matter of fact, there already is a class named Class. A variable containing a reference to an instance of this class often is called either clazz or cls - and that's what this question is about. And as Class is such a prominent member of Java, naming it ClassInformation or ClassMetadata wouldn't add much value - everybody knows what it does. It's one of these cases where a short name is sufficient and saves typing - much like the `$()` function that made this JavaScript guy a programming rock star :) – sfussenegger Mar 28 '10 at 09:03
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.

John
  • 9,254
  • 12
  • 54
  • 75
  • 3
    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* – usr-local-ΕΨΗΕΛΩΝ Dec 02 '14 at 11:25
7

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.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
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.

Roman
  • 64,384
  • 92
  • 238
  • 332
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.)

finnw
  • 47,861
  • 24
  • 143
  • 221
  • 3
    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. – finnw Mar 28 '10 at 01:11
  • Or `r#` in Rust. – leo848 Sep 10 '22 at 18:40
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

gmhk
  • 15,598
  • 27
  • 89
  • 112
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.

Romejanic
  • 419
  • 1
  • 4
  • 9