• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When Class names are instance names

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic