views:

348

answers:

6

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 ?

+20  A: 

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

GregS
Yeah, I know , but I think spell a variable right would be more meaningful.
ZZcat
@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
@Tony, you simply can't name a variable "class", it's a reserved word.
Steve Kuo
Class isn't reserved, only class.
Malfist
@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' ?
ZZcat
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
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
+2  A: 

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
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
You did not name a class Class , it's java API.
ZZcat
@Blindy: What about java.lang.Class? You don't have much choice there.
Dan Dyer
@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)
Milan Ramaiya
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
@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
@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
Fair enough, I'm just saying this looks incredibly weird and uninformative (not to mention inaccurate).
Blindy
@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
@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
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
@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
A: 

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

harigm
+1  A: 

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
+7  A: 

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 bother English and American) 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
A: 

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
what about $class in java?
irreputable
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