Packages in Java

Java Packages

What is a Java Package

A package is a collection of classes and the related functions within the classes.
Packages are container for classes that are used to keep the class name space compartmentalized. For example, you can create a class named “Abc” in your package “mypackage” without being concerned to it being colliding with other class also named as “Abc”.  And then you can write your own methods in it for the later use by you or anyone that uses that package.
Packages are stored in a hierarchical way. That means you can create a package inside a package. This is like a box contained inside a box.
Now how to create a package? Well this is simple, you just need to use the keyword ‘package’ followed by the name of the package.
This is the general form of the package statement:
package mypackage;

Constructors in Java

Constructors
A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined, the constructor is automatically called immediately after the object is created, before the new operator completes. Constructors look a little strange because they have no return type, not even void. This is because the implicit return type of a class’ constructor is the class type itself. It is the constructor’s job to initialize the internal state of an object so that the code creating an instance will have a fully initialized, usable object immediately.

EXAMPLE:

Using the Command Line Arguements

Command Line Arguements
Sometimes, there might arise a need for a program to run it in accordance with the specific input provided at the time of running the program. Command line arguements serve this purpose in java by allowing the user to input some values at the time of running the java program.
As the name implies, this feature is for only the programs which run on/from the command line interface. The input provided at the command line along with the instruction of running the java application, is stored in a String array and can be used in the program to make changes in the way program will behave.
Syntax:
They are directly passed to the main() of the java program.
public static void main(String args[])
the args[] is an array of strings, and it is called as command line arguements because the value of args[] can only be given at the run time from the command line. But wonder how to use it in your program?
This is simple, take a loot at the demo code that makes use of the command line arguements.



//Demo Code for using the Command Line Arguements in your program

Structure of Java Program

Source File Layout/ Java Program Structure
A Java Technology source file takes the following form:
[<package_declaration>]
<import_declaration>*
<class_declaration>+

NOTE: - The plus (+) indicates one or more. To be meaningful, a source file must contain at least one class definition.
1.     The order of the items is important. That is, any import statements must precede all class declarations and, if you use a package declaration, it must precede both the classes and imports.
2.     The name of the source file must be the same as the name of the public class declaration in that file. A source file can include more than one class declaration, but only one class can be declared public.
3.     If a source file contains no public class declarations, then the name of the source file is not restricted. However, it is good practice to have one source file for every class declaration and the name of the file is identical to the name of the class.

Declaring Classes in Java
The Java Technology class declaration takes the following form:
<modifier>* class <class_name> {

First Java Program


Getting Started with Java: Here is a simple Java program that just displays a custom message on screen say “Hello this is my first Java Program”.
I first write the program and then discuss the various parts of it.

Code:
//first java program
class FirstJava
{
  public static void main(String args[])
   {
     System.out.println("This is my first java program");
   }
}
 
Compile the code: 

Installing the JDK and Setting the Environment Variables

Want to get started with Java and set up your computer for Java, then this post is for you only. Every Java programmer I know starts off with writing the Java programs in any text editor such Notepad and compile and run it using Java tools on command line. But to make sure all goes well, you must need to install Java properly and Set up the environment variables for Java. 

Installing the Java 2 SDK (or JDK)

If you don’t have the Java 2 SDK(Software Development Kit) or simply JDK installed on your system, Follow these steps to install the JDK on your system:
1. Download the latest Java SDK version from the Oracle official site.

2. Now browse the jdk setup file on your computer and double-click on the setup file to open it. Now follow the on screen instructions to install the JDK on your system. JRE is also installed along with the JDK.
3. You can check if the JDK has been correctly installed or not by looking in the C:\Program Files\Java\. You must see the jdk1.6.0_26 folder and jre1.6.0_26 folder in it. 1.6.0_26 is the version of the jdk and it can be different depending upon which version did you download from the site.
4. Now you have successfully installed the JDK, the next step is to set the Environment Variables for your Java (JDK).


Setting up the Environment Variables


1. Right click on My Computer and click on “Properties”. Then in