• Post Reply Bookmark Topic Watch Topic
  • New Topic

can Java keywords be used as class name ?  RSS feed

 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to use while keyword as class name but can use  While ( capitalized W ) as class name . Can Java keywords be used as class name ?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've already answered your own question.

Java is case-sensitive so "While" is not the same as "while", and "while" is a keyword but "While" is not a keyword.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a comprehensive list of the Java keywords. As far as possible avoid using them, even with camel case escape strategies as pointed out by Jesper.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Maneesh Godbole  yes i understood but one last question in this thread what is camel case escape strategy ? I cannot find good information from other source
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See CamelCase

Maneesh means that it is not a good idea to use Java keywords as classnames, just by changing the case, because it will be confusing for other programmers. For example, don't call a class "While".
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, just to hammer it home, Class names that adhere to standard Java conventions begin with a capital letter, all of Java's keywords are completely lower-case, Java is case-sensitive, Q.E.D.

Aside from that, uppercasing a keyword to make it a conventional class name is confusing and can interfere with introspective and other class-managing systems. A case in point can be seen in JSF, where instead of the CSS keyword "class=" on JSF tags, JSF re-spells it at "styleClass". This is mostly for the benefit of the classes that will be implementing the CSS class attribute.

You may also notice that informally, it's common when writing code that introspects classes to define the variable that references the Class in question under the name "clazz", since naming a variable "class" would result in a compile error.
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java source code is case sensitive, i.e. While is not the same as while.
You can use While, because it's not a keyword in java, but while is a keyword.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jam Rei wrote:...You can use While...
But you should not. Jesper has mentioned the reason for.

Jesper de Jong wrote:Maneesh means that it is not a good idea to use Java keywords as classnames, just by changing the case, because it will be confusing for other programmers. For example, don't call a class "While".
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a separate note, a good program is not that which is understood by the machine.
A good program is that which is easily understood by other programmers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!