1) create a directory structure matching the package name for your source file relative to the source root.
2) put the package statement at the top of the file
for example the first line of org/example/myproject/MyClass.java is "package org.example.myproject;"
traditionally, package names start with a reversed domain name of the company or organization who wrote it.
2016-05-27 07:45:06 UTC
Java packages don't have their own files. Java source files need to have the package name at the beginning like so: package pack1; and arranged in folders according to package hierarchy. Do not follow a package statement with braces. Neeraj Yadav is probably thinking of C# namespaces.
ram
2009-12-28 23:40:38 UTC
you have two ways to create packages in java.
1) just write the following statement as the very first line of your java program(i.e., before importing anything)
package packagename;
this is the simple way to create a package
2) the second and traditional way to create a package is as follows
package com.packagename;
if you have any subpackages you can also give it as
package com.packagename.subpackagename
this is just like a website address which follows reverse order.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.