How to create packages in Java?
Creating packages in java is more than writing a single line in your java file
package packageName;
There are certain things to jot down in your mind before creating a package. A package name has the following format.
string(.string)*
It signifies that a package name consists of one or more strings separated by a period (.). Take for example
tutorials tutorials.wmessage tutorials.sutils tutorials.wmessage.translate
But to use a package, the above definition will not suffice. The modified form is this
dir(.subdir)*
which means that a package name corresponds to the relative path of a Class. Here dir corresponds to the main directory of the package you are creating and subdir correspond to the subdirectories within that direcrtory.
Let’s explore this more. If you write the following line in Tutorial.java
package tutorial;
It is mandatory that the file Tutorial.java is defined in tutorial/Tutorial.java.
Similarly a line in a java class
package tutorial.sutils;
means that it is defined in tutorial/sutils.
Let’s explore more on this with example. I am going to create two classes WelcomeString.java and StringUtils.java
$ mkdir tutorials $ mkdir tutorials/sutils $ mkdir tutorials/wmessage $ touch tutorials/sutils/StringUtils.java #creating a blank java file $ touch tutorials/wmessage/WelcomeMessage.java #creating a blank java file
Let’s see the file tutorials/sutils/StringUtils.java
package tutorials.sutils;
/** Set of Operations related to String
*/
public class StringUtils {
/*Concatenates two strings*/
public static String stringConcatenate (String str1, String str2) {
return str1+str2;
}
}
Note the line
package tutorials.sutils;
It is defined in tutorials/sutils, hence the name.
Let’s define tutorials/wmessage/WelcomeMessage.java
package tutorials.wmessage;
/** To deal with Welcome Messages
*/
public class WelcomeMessage {
/* Default Message prepended before any welcome message*/
String message = "Welcome to the world of Java programming\n";
/*Uses the default Welcome Message*/
public WelcomeMessage() {
}
/*Prepends the default welcome message to the user specified message*/
public WelcomeMessage (String message){
this.message += message; /*Concatenation of the two message*/
}
/*Prints the Welcome message*/
public void print(){
System.out.println(message);
}
/*Returns the Welcome Message*/
public String welcomeMessageString(){
return message;
}
}
See the first line
package tutorials.wmessage;
As mentioned above, it is defined in tutorials/wmessage, so the package name tutorials.wmessage.
Note that all the files in a directory will have the same name.
Tags: Class, create java packages, create packages, jar files, Java, java jar, java package, package, Welcome Message
Comments:
On Facebook
Translate









Search
Custom SearchRecent Comments
- Ash on log4cpp::PatternLayout – Available log4cpp log formats
- Joys of Programming on Curl / libCurl Tutorials with Examples
- Malik89 on Curl / libCurl Tutorials with Examples
- Matt on How to install java library for jogl in Ubuntu/Linux?
- Gokulnath on How to install Readline/libreadline in Ubuntu?
On Twitter
Tutorials
Twitter
- And you thought Eclipse is Just for Java Development. You can also use Eclipse for C/C++ development http://t.co/swtOLbkr 3 months ago
- For the C fans here, let's see a simple question. Is there any difference between func() and func(void)? http://t.co/ASTsWVc9 3 months ago
- ISO C++11 released. http://t.co/DyJ0rSa1 3 months ago
- RT @newsycombinator: IE team sends a cupcake for FireFox 5 http://j.mp/lS7pGF 7 months ago
- RT @newsycombinator: Voice search enabled on Google homepage (Chrome) http://j.mp/m5KT8c 7 months ago
Categories