package keyword in java

The "package" keyword specifies the Java package in which the classes declared in a Java source file reside.

Example
  1. package com.mycompany.services;
  2.  
  3. public class MyCompanyService {
  4. }
package com.mycompany.services;
 
public class MyCompanyService {
}


In the above example, the source file MyCompanyService.java must be placed under the package /com/mycompany/services

Rules regarding the package statement


A source file can be placed under a specific package or default package.

The package statement (if specified) must be the first line in the source file.

Package name must follow the pattern: level1.leve2.level3….leveln
where,
level1.leve2.level3….leveln are the sub packages in the same order.

If a Java source file does not contain a package statement, the classes defined in the file are in the default package.

Note that classes in the default package can not be referenced from classes in non−default packages.

In the example above, the fully−qualified class name of the MyCompanyService class is com.mycompany.services.MyCompanyService

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on