• Post Reply Bookmark Topic Watch Topic
  • New Topic

keyword used as variable name  RSS feed

 
J C upadhyay
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class names are not keywords.

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.9
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I got it...Thanks to all of you.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!