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:
Related
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
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.
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
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
I've seen lots of examples like com.mycompany.someapp. Seems to be the reverse of the domain. Which actually makes sense to me.
But at the end of the day, does it really matter? We are a small shop so maybe we don't see the benefits of proper domain naming.
So, is it good practice to name it to match the domain? If so, why?
Extracted from the link to Naming a Package (Java Tutorial) in Andrew's comment: (I claim no originality or ownership of the following).
Naming a Package
With programmers worldwide writing classes and interfaces using the Java programming language, it is likely that many programmers will use the same name for different types. In fact, the previous example does just that: It defines a Rectangle class when there is already a Rectangle class in the java.awt package. Still, the compiler allows both classes to have the same name if they are in different packages. The fully qualified name of each Rectangle class includes the package name. That is, the fully qualified name of the Rectangle class in the graphics package is graphics.Rectangle, and the fully qualified name of the Rectangle class in the java.awt package is java.awt.Rectangle.
This works well unless two independent programmers use the same name for their packages. What prevents this problem?
Naming Conventions
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).
Packages in the Java language itself begin with java. or javax.
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:
Legalizing Package Names Domain Name Package Name Prefix
hyphenated-name.example.org org.example.hyphenated_name
example.int int_.example
123name.example.com com.example._123name
Happy coding.
Matching the domain gives you greater confidence against name collisions. It's probably more important to designers of 3rd party libraries than you and your app.
Yes, that's the suggested convention in the Java Language Specification, section 7.7.
If unique package names are not used, then package name conflicts may arise far from the point of creation of either of the conflicting packages. This may create a situation that is difficult or impossible for the user or programmer to resolve. The class ClassLoader can be used to isolate packages with the same name from each other in those cases where the packages will have constrained interactions, but not in a way that is transparent to a naïve program.
You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as sun.com. You then reverse this name, component by component, to obtain, in this example, com.sun, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names.
You don't have to follow the convention, but it's generally considered good practice. After all, suppose at some point in the future you want to release some of your code as open source - but you want to avoid naming collisions. At that point, you really ought to follow the same conventions as everyone else - and as it doesn't hurt to do so from the start...
The idea behind using domain name is to avoid namespace collisions in packaging. This only works if everyone follows the convention. So, yes, the convention is important. That said, if you never plan on exporting your code as an API or providing it to a third party, it's likely there is little downside to using whatever package name you feel like.
Practically speaking I like it for a number of reasons:
It gives users an easy place to go to just from looking at the package name
It avoids collisions between packet names (i.e. two "media" packages could be very likely otherwise)
It helps identify the same author over separate pieces of software
It keeps package names roughly the same length (ok, this is just an aesthetic point but I like it!)
As well as this, it's also recommended in the JLS. It's not a requirement, but when it's practically 0 effort to do, I'd do it unless there's a good reason otherwise.
Perhaps a better question to ask is why don't you want to follow that convention? If there's no real reason, there's no harm in following it!
The main aim is to guarantee uniqueness of package names, but if you're never going to release code for others to use then it probably doesn't matter, but there is a lot to be said for sticking with convention and worrying about the stuff that does matter. Otherwise come the day that you realise you have a great library that you want to share you could be kicking yourself for going against the flow.
Yes, it is sensible to always use a naming scheme. As a counter-example, assume that everyone would use the default package for their classes.
Common classes like User or Address would be used by several libraries, but in the end there can be only one class of a certain name in the runtime environment. (loosely speaking, it is not completely correct.)
In big projects you will likely use many external libraries, like Apache Commons, Google Guava, Spring, Hibernate, Terracotta. It's good that these libraries all use their own namespace, so that their internal classes do not accidentally conflict.