Java, why can't we use null as package name? - java

I am porting apitest to jogl, one of the directory is called Null, so I was simply trying to use null as package name and Netbeans complains that it is not valid.
I know I am splitting hairs, but I am just curious.. I didn't find anything here or on google..
Does it have to do with the reflection, the name retrieving, something similar or something else?
Or is it like this by design, same as operator overload?
Edit: I know it is a reserved word, but in code..

An identifier cannot be named null as the language specification mentions:
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), or a compile-time error occurs.

in this case, null is a literal (null literal)in java language, you can not used as identifier...
According to Oracle:
Naming Convention:
Package names are written in all lower case to avoid conflict with the
names of classes or interfaces.
...
In your case Following reference is important:
In some cases, the internet domain name may not be a valid package
name. This can occur if the domain name contains a hyphen or other
special character, if the package name begins with a digit or other
character that is illegal to use as the beginning of a Java name, or
if the package name contains a reserved Java keyword, such as "int".
In this event, the suggested convention is to add an underscore. For
example:

I believe it is like this only by design.
As per Java Language Specification, 'null' is a keyword and you cannot use any of the keywords to identify any other thing than its prime purpose.
Although you can use Null with capitalized 'N' and similar for all other keywords that does not match case with predefined keywords.
Java discourages use of such poor naming practices.

null is a value given to identifier. package is a identifier. it cannot be a value.

Related

Can not use .in domain name in package

I tried to search on stack overflow regarding the issue I am facing but did not find any satisfactory answer. So, please read my question first and suggest.
I have a .co.in domain that I want to use to prepare packages. But as per new java conventions(?) the keyword 'in' can not be used at starting of package name. Hence, I am getting problems in building applications.
I have problem in building android application with flutter due to package name format in.co.mydomain.myapp.
The problem persists in JavaFX applications also where I am using hibernate ORM. When application is run in debug mode, I see HQL queries generated with fully qualified names of entity class resulting into in.co.mydomain.myapp.entities.Student, here also 'in' keyword is a reserved keyword of SQL queries hence it throws errors.
I need expert advice in such a horrible situation I am facing.
The whole thing works without any problem when I rename package to com.mydomain.myapp
In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore.
https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
So you could use in_.co.mydomain
oracle suggested that,
In some cases, the internet domain name may not be a valid package
name. This can occur if the domain name contains a hyphen or other
special character, if the package name begins with a digit or other
character that is illegal to use as the beginning of a Java name, or
if the package name contains a reserved Java keyword, such as "int".
In this event, the suggested convention is to add an underscore. For
example:

Package names should comply with a naming convention (squid:S00120) and underscore

SonarLint produces an error:
Package names should comply with a naming convention (squid:S00120) ... With the default regular expression ^[a-z]+(\.[a-z][a-z0-9]*)*$.
However the JLS allows and even recommends using the underscore. Clause 6.1. Declarations says, i.a.,
If the domain name contains a hyphen, or any other special character
not allowed in an identifier (§3.8), convert it into an underscore.
Can I change the regular expression or must it be fixed in the product?
Consider using a SonarQube server. You can connect your SonarLint plugin to the server and manipulate the way SL performs SCA in your IDE.
One of the nicer features of SQ is the ability to disable SCA rules and remove them from all future scan. Using this feature, you can disable the package name rule (which I also find annoying),a and not have it clutter up the editing view.
Another option is to create a new rule, which will enforce a new regular expression convention on the package names - for example, allowing package names to be Camel Case.
Finally, as stated by Jonathan Rosenne in a comment to the question, the package name issue SONARJAVA-2596 which Jonathan had opened following this question has been fixed, as can be seen here.
Reference:
How to disable a rule in SonarQube
Writing Custom Java Rules 101

Java package naming. Underscores: A special case

Today I was naming a package in a project which would contain code related to a concept called an "access structure".
Now, naming this package "com.myemployer.project.component.accessstructures" seems unappealing and difficult to read because of the triple "S". (The higher level packages are not actually named "project" and "component").
I was tempted to use "...component.access_structures"
I couldn't find anything mentioned in the Java conventions on Oracle's site . And a brief web search brought up nothing.
What is the official convention for names like this?
From Oracle Docs
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.
Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage).
In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore
Although this text doesn't specify your exact case, it does say that for an invalid package name we should use an underscore. One could argue that accessStructures is how we would define a method in Java and thus naming a package like that could be confusing.
Overall, it is really up to you.
If you want to keep with this convention, I believe you should name your package:
com.myemployer.project.component.access_structures
Also you can look up synonyms and find alternatives that would give less confusion. Some I quickly found:
accessframework
accessfactory
accessarch (accessarchitecture)
accessconstructs
I dont think there is any standard for that. People follow different naming conventions as per there convenience and readability. But most of the programmers find camel case naming as the most convenient. You can name it like accessStructure
Found one Oracle Doc which recommends to use the package name with all small letters
Package names are written in all lower case to avoid conflict with the
names of classes or interfaces.
According to docs you can't use camelCase for package naming. It's ok to use snake_case for package naming in some cases, but it is more appropriate if you can't use your domain properly, because of the hyphen sign in it or it starts with numbers. But it has to be rather an exception from the rule than the rule.
If I were you I would rephrase it. For example: accessstructures -> accesscore

Eclipse Java - invalid package name - Reserved words in package name

I am in the middle of an android project and was trying to create a new package in it.
com.mycompany.myprojectname.new
Well, Eclipse is not letting me to create it and is showing this error:
Invalid package name. 'new' is not a valid Java identifier
I never knew package name has reserved words, which we cannot use. My questions are;
Is this an Eclipse thing? or a Java thing? I tried a pure Java project
(not Android), just to check, but there also I got the same error.
What are other reserved words that is not allowed?
Is there any documentation about this?
Yes, this is a general Java thing.
The list of reserved words can be found here. They are:
abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
Documentation on the fact that reserved words can not be used in package names if found in the package naming tutorial, among other places.
The authoritative source is (as always) the Java Language Specification, specifically:
§ 3.9 Keywords and
§ 3.8 Identifiers
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), or a compile-time error occurs.
See docs here:
http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html
In some cases, the internet domain name may not be a valid package
name. This can occur if the domain name contains a hyphen or other
special character, if the package name begins with a digit or other
character that is illegal to use as the beginning of a Java name, or
if the package name contains a reserved Java keyword, such as "int".
new is a java keyword. Use some other word instead of it.
YMMV but be careful naming packages target; may mess with your code repository and/or IDE

Javabean convention - method naming for property gId

If I have a property 'gId' in my Java class what should the accessor method be named as?
getGId is what I assume.
If there were a property gURL I think it would be getGURL, which kind of looks ugly (not referring to the alternative spelling of girl though).
If the property was just url the method name getUrl is good on the eye and yeah I would not name the property as URL in the first place which would make the accessor ugly again - getURL
I remember reading from the Javabean Specification PDF somewhere about capitalization of properties and also cases involving acronyms but cant find it in the PDF anymore.
Does anyone have a reference to it or please confirm if I am right in what I am saying?
The reference you are interested in can be found in the Beans specification at Section 8.8.
That being said, it does not explicitly cover your particular case of gId/gURL. The specification says that to provide a getter/setter, we simply capitalize the first letter. To recover the property from the getter/setter, if the first two letters are uppercase, then the whole property is left as-is. Otherwise we decapitalize the first letter. So your getter would become getGURL, but your property would be incorrectly recovered from the getter as GURL. You have the same problem with gId.
Therefore it seems that the specification does not allow you to provide a consistent translation of any property with a first lowercase character followed by an uppercase character.
My suggestion is to either adopt a completely lowercase property, or to extend the lowercase prefix to two letters (glURL, for example).
To be more concret (and as said by Luca and here), spec tells that there is a method which can tell you: java.beans.Introspector.decapitalize(String).

Categories