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.
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.
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment