• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When Class names are instance names

 
Kevin Crays
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While going through Generics in the K&B book, I discovered (or did I rediscover?) that

is legal.

I've worked with java off and on for years, and I never would have thought this was legal. Thus, when I got

to compile, I was certain I was misunderstanding generics (off to google, followed by a trip to Barnes and Noble, I went).

Is this featurediscussed in K&B or in other books? Maybe it's me, but it just seems odd that a Class and variable can have the same exact name.
 
Jacky Zhang
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class X { public <X> X(X x) { } }
1st X class name
2nd X generics placeholder/declaration
3rd X constructor name
4th X a type
little x, identifier
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That particular example with class X is discussed on page 604 in K & B. The compiler is not confused because the identifiers are used in different contexts, class name, type parameter, and variable identifier.
 
Kevin Crays
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand, but forgetting about Generics, is it discussed either in K&B or elsewhere that

Integer Integer = New Integer() is legal?

This seems like something they'd put on the test (though they may not), yet I only realized it was legal by accident.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kevin Crays:
... This seems like something they'd put on the test...

Yes, you should expect to be tested on what is legal for an identifier.
An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. An identifier cannot have the same spelling (Unicode character sequence) as a keyword (�3.9), boolean literal (�3.10.3), or the null literal (�3.10.7).

Ref: JLS 3.8 - Identifiers

Note: "Same spelling (Unicode character sequence)" implies case sensitivity.
 
Kevin Crays
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc. I knew the definition, but in my mind Integer Integer = 1; would nevertheless cause a compiler error.

Oh well....at least I learned something today
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kevin Crays:
Hi Marc. I knew the definition, but in my mind Integer Integer = 1; would nevertheless cause a compiler error.

Oh well....at least I learned something today

Yeah, Java allows plenty of room to make really bad naming choices. Be careful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic