• 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

keyword used as variable name

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the following statement:
Number Float=99.4f;
Number String=99;

It compiles successfully despite using Float which is a wrapper classname as a variable name. Why is it so?
Can wrapper classnames be used as variable names ? IF yes, then why?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class names are not keywords.

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.9
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does Java know whether am referring to a variable named String or a the String class?

Just tried the snippet below, can you please throw some light on this.



Thanks,
Praveen
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote:How does Java know whether am referring to a variable named String or a the String class?



Context.




String #1 can only be a type name. You cannot put a variable name in that spot.
String #2 can only be a type name. You cannot put a variable name in that spot.
String #3 can only be a variable name. You cannot put a type name in that spot.
String #4 can only be a varaibel name. You cannot put a type name in that spot.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get that...but what I wanted to know was when I put in a statement to print out Integer.MAX_VALUE, the compiler threw an error. It was referring to the variable rather than the class, which seemed a little iffy because I could still use Integer as a class when I created a reference of it.

When you said Context, am assuming its the general method/block context of a program.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen Kumar M K wrote: I get that...but what I wanted to know was when I put in a statement to print out Integer.MAX_VALUE, the compiler threw an error. It was referring to the variable rather than the class, which seemed a little iffy because I could still use Integer as a class when I created a reference of it.



It seems in contexts where either a variable name or a class name would be acceptable (such as Integer.MAX_VALUE), it assumes that you are referring to the variable. That makes sense, because if you need to use the class, you can use the fully qualified class name--java.lang.Integer.

Integer still works as a class name for things like declaring a variable, since in that context it can only be a type name, not a variable name.

When you said Context, am assuming its the general method/block context of a program.



What I meant by context was what I showed in my example code.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.5.2
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Jeff. I tried a few more scenarios and got expected errors, you're right about the usage of the fully qualified class name in ambiguity.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The language can't possibly have a rule which says "You can't use a name for a variable if it's already the name of a class", because there is no restriction on what names people could give to classes they create. Otherwise I could write a class named Grama and then you couldn't use a variable named Grama. And it can't possibly have a rule which says "You can't use a name for a variable if it's already the name of a wrapper class", because that would be a silly rule -- why restrict the rule to those classes?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is covered in Java Puzzlers by Bloch and Gafter; a name used as the name of a variable takes precedence over the name of a class. That is why you ought to stick to the conventions about names. I think there is a misprint in that link; they have written variable for constant and vice versa in one place.
 
J C upadhyay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it...Thanks to all of you.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic