Command line argument in Java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.Command line argument in Java
The command line argument is the argument passed to a program at the time when you run it. To access the command-line argument inside a java program is quite easy, they are stored as a string in String array passed to the args parameter of a method
main()
.
Example
class cmd { public static void main(String[] args) { for(int i=0;i< args.length;i++) { System.out.println(args[i]); } } }
Output :
10 20 30
Java Package
The package is used in Java, in order to avoid name conflicts and to control access of class, interface, and enumeration etc. A package can be defined as a group of similar types of classes, interface, enumeration, and sub-package. Using package it becomes easier to locate the related classes.
The package is categorized into two forms
- Built-in Package:-Existing Java package for example,
java.lang
java.util
etc. - User-defined-package:- Java package created by the user to categorized classes and interface
Creating a package
Creating a package in Java is quite easy. Simply include a package command followed by name of the package as the first statement in java source file.
package mypack; public class employee { ...statement; }The above statement creates a package called mypack.
Java uses a file system directory to store package. For example, the
.class
for any classes you to define to be part of mypackpackage must be stored in a directory called mypackExample of java package
//save as FirstProgram.java package LearnJava; public class FirstProgram{ public static void main(String args[]){ System.out.println("Welcome to package"); } }
How to compile java package?
If you are not using any IDE, you need to follow the syntax given below:
javac -d directory javafilename
Example:
javac -d . FirstProgram.java
The -d switch specifies the destination where to put the generated class file. You can use any directory name like d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use. (dot).
How to run java package program?
You need to use fully qualified name e.g. LearnJava.FirstProgram etc to run the class.
To Compile javac - d. FirstProgram.java
To Run: java LearnJava.FirstProgram
Output: Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.
import keyword
import keyword is used to import built-in and user-defined packages into your java source file. So that your class can refer to a class that is in another package by directly using its name.
There are 3 different ways to refer to a class that is present in the different package
- Using fully qualified name(But this is not a good practice.)If you use fully qualified name then only declared a class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.It is generally used when two packages have the same class name e.g. java.util and java.sql packages contain Date class.Example :
//save by A.java package pack; public class A{ public void msg(){System.out.println("Hello");} } //save by B.java package mypack; class B{ public static void main(String args[]){ pack.A obj = new pack.A();//using fully qualified name obj.msg(); } }
Output:
Hello
- import the only class you want to use(Using package name.classname)If you import package. the class name then only declared a class of this package will be accessible.Example :
//save by A.java package pack; public class A{ public void msg(){ System.out.println("Hello"); } } //save by B.java package mypack; import pack.A; class B{ public static void main(String args[]){ A obj = new A(); obj.msg(); } }
Output:
Hello
- import all the classes from the particular package(Using package name.*)If you use package.* then all the classes and interfaces of this package will be accessible but not sub-packages.The import keyword is used to make the classes and interface of another package accessible to the current package.Example :
//save by First.java package LearnJava; public class First{ public void msg(){System.out.println("Hello");} } //save by Second.java package Java; import Learnjava.*; class Second{ public static void main(String args[]){ First obj = new First(); obj.msg(); } }
Output:Hello
Points to remember
- When a package name is not specified, a class is into the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
- While creating a package, care should be taken that the statement for creating package must be written before any other import statements.
// not allowed import package p1.*; package p3;
//correct syntax package p3; import package p1.*;
Comments
Post a Comment