Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numbers as package names in java

Tags:

java

package

I have checked the following posts already:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.2

and SO post:

Is this a valid java package name?

and

What package naming convention do you use for personal/hobby projects in Java?

I tried creating a package like this:

package 01;
public class Test
{
// ...
}

When I compile I get this:

I:\code>javac 01\Test.java
01\Test.java:1: error: <identifier> expected
package 01;
       ^
1 error

I am looking for syntactical correctness of the name not the convention.

Is there a oracle/SUN reference that lists out the syntax for a package or does it follow the same rules as for variable names in java? May be a reference in the Java Language Specification saying package names follow the same rules as variable names.

like image 282
Ayusman Avatar asked May 03 '15 16:05

Ayusman


People also ask

Can Java package name have numbers?

If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component. Save this answer.

Can package name contain numbers?

To clarify, you can name your app 1abcxyz, it's only the package name that cannot start with a digit (you could use oneabcxyz instead of 1abcxyz for example). Having a package name different from your company's website should not influence communications between your app and your company's servers.

How should I name packages in Java?

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 .

Can you have dashes in Java package names?

While using hyphens in the names of repository items is a recommended practice in AEM development, hyphens are illegal within Java package names.


2 Answers

Found it:

If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component.

In JLS 6.1 Declarations

like image 124
Luiggi Mendoza Avatar answered Oct 15 '22 11:10

Luiggi Mendoza


This links to identifier grammar that Java uses, which applies everywhere identifier is mentioned, including but not limited to package name. So 01 is definitely not a valid package name, since it's not a valid identifier.

like image 45
LeleDumbo Avatar answered Oct 15 '22 10:10

LeleDumbo