Thursday, October 4, 2012
 Reasons for introducing the concept of packages in Java-->
1 : for better organizing of resources
2 : to avoid class name conflicts i.e. if there were no packages then it wouldn't have been possible to declare duplicate class names;
Convention of declaring packages--> package names are generally given according to reverse domain name. e.g.if your domain is javaingrab.blogspot.com then package is com.blogspot.javaingrab followed by your project and so on. This will prevent package name conflicts.
How to use packages--> First of all you will have to create a directory structure according to packages. e.g. for package com.blogspot.javaingrab directory structure is com/blogspot/javaingrab and so on. There is a keyword in java called package to declare a package. Now package statement should be the first line of your code. take a look at following code

package com.blogspot.javaingrab.xamples;
public class MyClass{
   public MyClass(){
      Sysytem.out.println("Package Demo");
   }
   public static void main(String[] args){
       new MyClass();
   }
}

How to compile with packages--> Generally sources are kept in a separate folder named src and classes in classes directory.Considering above example the directory would be like this /project/classes and /project/src/ and within src it will be src/com/blogspot/javaingrab/xamples and inside it will be MyClass.java file. Now if you are compiling from project directory command will be
C:\project javac -d classes -cp src src\com\blogspot\javaingrab\xamples\MyClass.java
-d switch : it is optional and is used if you want to place class files in a separate directory. This helps in automatic creation of directories according to your packages.
-cp switch : it means classpath. This is necessary only when your source requires other classes which may or may not reside in the same packages. Using this you can compile a particular class even if the dependent class resides in a different drive.
How to run with packages--> While running you will have to give the -cp switch followed by the dependent class file directory and then the fully qualified class name with package which you want to run. for the above class the command should be
C:\project java -cp classes com.blogspot.javaingrab.xamples.MyClass

WARNING : Do not mention about any package named directory as java compiler or JVM searches for the particular package inside the classpath. Yf you do this then it will be disastrous,so be careful.

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.