wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When Class names are instance names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When Class names are instance names" Watch "When Class names are instance names" New topic
Author

When Class names are instance names

Kevin Crays
Ranch Hand

Joined: Apr 26, 2006
Posts: 41
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.


We're binary code: a one and a zero<br />You wanted violins and you got Nero
Jacky Zhang
Greenhorn

Joined: Sep 17, 2006
Posts: 18
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Apr 26, 2006
Posts: 41
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Kevin Crays
Ranch Hand

Joined: Apr 26, 2006
Posts: 41
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

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When Class names are instance names
 
Similar Threads
generics
Generics
Generics
generics
Generics