Subscribe to my Youtube channel

Monday, August 30, 2010

Package's in Java

Introduction

           A package is a namespace that organizes a set of related classes and interfaces to provide access protection and namespace management.You can think of packages as being similar to different folders on your computer. Software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages. JDK has set of previously defined packages consisting of relevant classes.E.g IO package contains all the classes related to input output. Moreover, many times when we get a chance to work on a small project, one thing we intend to do is to put all java files into one single directory. It is quick, easy and harmless. However if our small project gets bigger, and the number of files is increasing, putting all these files into the same directory would be a nightmare for us. In java we can avoid this sort of problem by using Packages.

Benefits:-
  •  We can easily determine that the classes and interfaces in a single package are related.
  • The names of your classes and interfaces won't conflict with the names in other packages because the package creates a new namespace.
  • You can allow classes within the package to have unrestricted access to one another yet still restrict access for types outside the package.
Package Declaration:-       

  To create a package, you choose a name for the package and put a package statement with that name at the top of every source file that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. If you do not use a package statement, your type ends up in an unnamed package (Use an unnamed package only for small or temporary applications).

Syntax:
             package  packagename ;
e.g:- package MyPackage;

This should be the first statement of the source file before any imports and class declarations.

Example:-
Code:
-----------------------------------------------------------------------------
package MyPackage;
class xx
{
    ........
    .........
}
class yy
{
    public static void main(String args[])
    {
       . . ......
        ......
     }
}
--------------------------------------------------------------------------------
          then a directory called MyPackage should be created under the actual directory and the file should stay in that directory. When this particular file is saved it should be saved as yy.java within the directory MyPackage. So when it is compiled the .class files xx.class and yy.class are automatically going to stay in MyPackage directory.
         A Hierarchy of packages may be created. To do so, simply each package name should be separated from the one above it by use of a period.

package pkg1[.pkg2[.pkg3]]];
but  it should also stay in the corresponding directory.

For eg. a package declared as Package java.awt.image should stay in java\awt\image directory.
 
         Now, again if a particular. java file is made to stay in a directory because it is written as a part of a corresponding package, then the file should be executed accordingly. For eg. the execution of yy.java in the earlier example should be done as

java MyPackage.yy
being in the actual directory i.e path just above the package.

An Example:-
code:
-------------------------------------------------------------------------------
// A simple package
package MyPackage;
class Balance
{
      String name;
      double bal;
      Balance(String n, double b)
     {
       name = n;
       bal = b;
     }
     void show( )
    {
        if(bal < 0)
        System.out.print(“-->; ”);
        System.out.println(name + “: $ ” + bal);
    }
}
class AccountBalance
{
    public static void main(String args[])
    {
        Balance current[] = new Balance[3];
        current[0] = new Balance(“K. J. Fielding”, 123.23);
        current[1] = new Balance(“Will Tell”, 157.02);
        current[0] = new Balance(“Tom Jackson”, -12.33);
         for (int i = 0; i < 3; i ++) current[i].show( );
     }
}
--------------------------------------------------------------------------------
     This file should be called AccountBalance.java and should be placed in a directory named MyPackage. Then after compilation the AccountBalance.class will stay in the MyPackage directory. However, if this does not happen by default then all the .class files should be placed in the MyPackage directory. Then the file should be executed by the command

java MyPackage.AccountBalance

variable is declared private then that variable could be accessed within that class only. It could not be accessed anywhere anyway outside that class. If a variable is made default(not declared public, private or protected) then the variable can be thought to be acting like a private variable outside the package i.e. outside the package that variable cannot be accessed neither from any subclass of that class nor from any other classes. In order to access that variable from outside the package, the variable has to be declared as public or protected. If it is public, then it can be accessed directly from any subclass of that class outside and through an object of that class from some other classes. However if the variable is declared as protected, then it can be accessed outside the package only from any subclass of that class. Moreover, a class has only two possible access levels:- default and public. When a class is declared as public, it is accessible by any other code. If a class has default access, then it can only be accessed by other code within its same package.

Sunday, August 29, 2010

What is necessity of Static memebers?

please follow the video

Tuesday, August 3, 2010

Writing a simple Java Program

            Java Programming is easier for who understands the programming languages C and C++. We all know that Java is Object Oriented Programming language. The reason for this is, the entire java programming is based on the objects.
            Java program is simply treated as a class or collection of classes. Class concept is similar to C++ classes. We discuss more concepts about java programming in further articles.
            In this article we mainly concentrated about the how to write a simple java program, compiling and executing java program. You can write java program in any text editor but you must save the file with extension “.java”.

Thursday, July 22, 2010

Setting Java Classpath



            After installing java, it needs setting PATH and  CLASSPATH .   Because of CLASSPATH, we can able to use java predefined classes which are provided by java software (Sun Microsystems..).
           Because of PATH, it provides a path for OS to execute commands. In PATH, we have to set absolute path for executable files.(generally bin folder). 
   Steps for Creating PATH and CLASSPATH
  •     Open MyComputer, in that on free space right click on mouse. it will open drop down menu, in that select Properties option.
              after click new it open window like the following
   

                 Next Step Now  follow the diagram ...
   

Thursday, July 15, 2010


Java Interpreter

               We can run Java on most platforms provided a platform must has a Java interpreter. That is why Java applications are platform independent. Java interpreter translates the Java bytecode into the code that can be understood by the Operating System
             Basically, A Java interpreter is a software that implements the Java virtual machine and runs Java applications. As the Java compiler compiles the source code into the Java bytecode, the same way the Java interpreter translates the Java bytecode into the code that can be understood by the Operating System.
           When a Java interpreter is installed on any platform that means it is JVM (Java virtual machine) enabled platform. It (Java Interpreter) performs all of the activities of the Java run-time system. It loads Java class files and interprets the compiled byte-code. You would be glad to know that some web browsers like Netscape and the Internet Explorer are Java enabled. This means that these browsers contain Java interpreter. With the help of this Java interpreter we download the Applets from the Internet or an intranet to run within a web browser. 
           The interpreter also serves as a specialized compiler in an implementation that supports dynamic or “just in time,” compilation which turns Java byte-code into native machine instructions.  Throughout Java programming, we’ll build both, the standalone Java programs and applets. Sun’s Java interpreter is called java
              Lets learn how to start a standalone application with it. Load an initial class and specify it. Some options can also be specified to the interpreter, and any command-line arguments needed for the application as well:
           % java [interpreter options] class name [program arguments]

    The class should be specified as a fully qualified class name including the class package, if any.
     Note : Moreover, we don’t include the
.class file extension. Here are a few examples:

         % java animals.birds.BigBird
         % java test


      Once the class is loaded, java follows a  C-like convention and searches for the class that contains a method called main(). If it finds an appropriate main() method, the interpreter starts the application by executing that method. From there, the application starts additional threads, reference other classes, and create its user interface.
         Now, lets see how to go about an Applet. Although Java applet is a compiled Java code, the Java interpreter can’t directly run them because they are used as part of a larger applications. For this we use Java Applet Viewer. It is a command line program to run Java applets. It is included in the SDK. It helps you to test an applet before you run it in a browser.
The Figure below shows the working of Java Interpreter: