• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Naming Convetions  RSS feed

 
Dilip H Pashupathi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to know if a java class name can be declared as "pack1.ClassName". I know the naming conventions rules and all. But this one was bothering me and I could't get straight answers from any one. I know that this kind of names are not supposed to be used. But I am just curious of whether Java accepts such kind of names also i want to know if $ClassName, _ClassName are valid for classname and are the being used anywhere in the application development

Thanks and Regards
Dilip
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dilip H Pashupathi wrote:I know the naming conventions rules and all.

Are you talking about rules or conventions.
Rules have to be followed.
Conventions are just generally accepted ways of doing things.

Dilip H Pashupathi wrote:I am just curious of whether Java accepts such kind of names

Why don't you create a class with such names and see if the compiler complains.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dilip H Pashupathi wrote:I want to know if a java class name can be declared as "pack1.ClassName".

Sure, it's just that usually there's no need. Assuming that 'pack1' is a package name, the normal way to avoid fully-qualified names is to use an import statement.

I know the naming conventions rules and all. But this one was bothering me and I could't get straight answers from any one. I know that this kind of names are not supposed to be used. But I am just curious of whether Java accepts such kind of names also i want to know if $ClassName, _ClassName are valid for classname and are the being used anywhere in the application development

Basically, any legal name is valid; however, the conventions are there for a reason, and things like underscores and dollar signs are usually imported from other languages like Basic and Python, where they have a specific meaning. They're basically redundant in Java, and simply likely to annoy people you work with.

Winston
 
Dilip H Pashupathi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all,

That helped.
@Joanne, I tried to create a class with such a name and tried to compile it but the compiler gave a warning saying "{" expected at "pack1.Classname" and the pointer indicated to "." place.

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file name would be ClassName.java and it would live in a folder called pack1 (or at least the ClassName.class file would).
You would compile it with
javac pack1/ClassName.java
and execute it with java pack1.ClassName (if it had a main method). ***
You would import it as import pack1.ClassName;*** or refer to it by the fully‑qualified name pack1.ClassName*** and getClass().getName() would return pack1.ClassName.
In the .java file you would writeYou can call it all sorts of names in normal writing but I have marked the three situations I can think of where you would use pack1.ClassName in programming with asterisks***.
I hope that answers your question.

[edit]Corrected 2nd occurrence of .java to .class because mistake noticed.[/edit]
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
$ also gets used internally by the compiler. Whenever you use an inner class, e.g.
If you look at the compiled classes, then you'll see a class called Outer$Inner has been created. And anonymous inner classes will compile down to something like Outer$1, Outer$2 etc. But you won't see them in the source code, and there's no reason to use them yourself. There's actually a line in the Language Specification that says:
The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dilip H Pashupathi wrote: but the compiler gave a warning saying "{" expected at "pack1.Classname" and the pointer indicated to "." place.

That wasn't a warning, it was an error. The basic class name can't contain '.' as it is used as a separator in the fully qualified class name (i.e. the combined package and class name)
 
Dilip H Pashupathi
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

That helped
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!