0

In JLS 15.8.2, it says

A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a . and the token class.

And in WIKI, it says

An expression in a programming language is a combination of explicit values, constants, variables, operators, and functions...

And I didn't see any operator similar to it in here

I know that String.class is an instance of Class<String>, I just don't understand why on earth we can write an expression like this.

So what is .class really and how to understand this notation?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
NYoung
  • 335
  • 3
  • 12
  • For all practical purposes, `String.class` is not _an_ instance of `Class` it is _the_ instance. Unless you are doing something very tricky, all of the strings in your program should belong to the same class (i.e., to the same `Class` instance). – Solomon Slow Aug 25 '15 at 13:48
  • There are a lot of annotation (which need static information), which needs a class information and you can give it like that (example TestNG): `@Test(expectedException = BlubException.class)`. And there are other reasons to use `XYZClass.class`. You may get a good answer with more examples. – Tom Aug 25 '15 at 13:49
  • 1
    I think everyone is misinterpreting the question. As far as I understand, he's asking why this is a valid notation when the list of operators doesn't include `.`. – Jeroen Vannevel Aug 25 '15 at 13:49
  • As for why, It just _is_. `Foo.class` has been part of the Java language since the very beginning, same as `a.length` where `a` is an array reference. – Solomon Slow Aug 25 '15 at 13:52
  • A guess: The Java designers probably wanted the naked name of a class to be a _type_ expression so that we could write declarations like `String s;` Allowing the same exact token to stand for a class literal in a different context would have made the formal syntax of the language more complex (harder to write a compiler), so they needed a different syntax to denote a class literal. How they hit upon _typename_`.class`, I do not know. I would have suggested `String.getClass()` (i.e., a static method call) if I could have been there at the time. – Solomon Slow Aug 25 '15 at 14:06
  • @james large: actually, they were introduced in Java1.1, so they are younger than `array.length`. – Holger Aug 28 '15 at 08:27

4 Answers4

2

String.class is a primary expression which does not decompose into operands and operators, but that doesn't stop it from having internal syntactic structure. I am sure you will agree that " is not an operator, either, yet "i am a string" is a literal expression.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

If there were a public field on object foo called bar, you could address that field with simply foo.bar. It's the same thing here - think of it as an implicit public static field called class.

dcsohl
  • 7,186
  • 1
  • 26
  • 44
0

The . is in fact an operator: it's the "dot operator". It's not mentioned in the official list of operators because.. who knows.

It is however mentioned in the official tutorial:

Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name,

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • For something to qualifiy for an operator it would have to be used only in the context where its left-hand and right-hand side are _operands_ and an operand must be an expression. So, unofficially we call it an operator, but formally it really isn't one. – Marko Topolnik Aug 25 '15 at 13:57
  • @MarkoTopolnik I'm not quite convinced that the left and right side aren't operands. Doesn't the first one access a class and the second a field? I'm not familiar with the Java terminology of the syntax tree but in C#'s Roslyn, those are "SimpleMemberAccessExpression" expressions. – Jeroen Vannevel Aug 25 '15 at 14:02
  • But i can't find a field name `class`. – NYoung Aug 25 '15 at 14:02
  • No, `String` is not an expression and `class` is not an expression. You cannot look up a field on something that doesn't even evaluate. – Marko Topolnik Aug 25 '15 at 14:03
  • @ChongYoung: _i can't find a field name class_ That's because it's magic. It's not a static field. It's part of the _language_. The compiler knows what .class means without ever having to look at the class object itself. – Solomon Slow Aug 25 '15 at 14:08
0

The syntax Foo.class is a shortcut for (new Foo()).getClass() without the memory allocation. It is useful any time a method wants to receive an instance of Class<?> as an argument.

Some examples in the standard library:

dsh
  • 12,037
  • 3
  • 33
  • 51