Sunday, January 30, 2011

JAVA Programming : Object

JAVA Objects


As you know from the previous post, class is a blueprint for object. So, an Object is an instance of class that Java Virtual Machine (JVM) create at runtime whenever it finds an new keyword. Every object will have state and behavior defined in its class. 
To understand software objects, first we look at some example of real world object like a Fan. A fan will have its own state like motor, blade, power switch etc. and behavior like speed, on or off etc. In the same way there are software objects that too have state in form of fields or variables and behavior in form of methods or functions.

Creating an Object:
For example we take a Television class and create its object.


public class Television {

    // the Television class has two fields
    public int size;
    public String  colorType;

    // the Television class has one constructor
    public Television(int tvSize, String tvColorType) {                      


        size = tvSize;
        colorType = tvColorType;
    }
}



 A object of television class can be created in this way:


                 Television tv = new Television(12,"Color TV");              


In a object Declaration Statement there is three part
1. Declaration of object : Part of statement before the '=' sign is Declaration part of object.
2. Instantiation : The new keyword in Java does the instantiation of an object.
3. Initialization : After new keyword there is call to construction of class which does the initialization.

It is not necessary that declaration part is to followed by instantiation and initialization part. Like other variable declarations, object declarations can be done alone. A object declaration can be done in this way :


                 Television tv;   
                 tv = new Television(12,"Color TV");                         


Wednesday, January 19, 2011

JAVA Programming : Class

JAVA Class


Class in Java is a template that describes the kind of state and behavior that objects of its type support. In simple words a Class is an blueprint from which an object is constructed. 
For Example think of a Blueprint of a house. A class for an object is like a blueprint for a house. A blueprint can be used to build a lot of hoses and a class can be used to build a lot of object.
Things defines inside the class are said to be member of the class and when the objects are constructed from the class, the member of class also become member of each object. 

A Class declaration can be done in this way:


public class Television {

    // the Television class has two fields
    public int size;
    public String  colorType;

    // the Television class has one constructor
    public Television(int tvSize, String tvColorType) {                      


        size = tvSize;
        colorType = tvColorType;
    }

    // the Television class has two methods
    public void setSize(int newValue) {

        size = newValue;
    }


    public void setColorType(String newValue) {

        colorType = newValue;
    }
}



Java Subclass :
Subclass in Java is a class that is an extension of an existing class. In other words Subclass is a class that extends another class. A Subclass is also known as Derived Class or Child Class.
A Subclass will inherits all properties like variables and methods of Extended Class or Super Class.
For example, we make a Class LcdTv which extends the class Television. Now the LcdTv class will have all the properties of Television so we don't need to write them again but LcdTv class has one extra property contrastRatio that is not present in Television Class, so we need to declare that field in LcdTv class.


public class LcdTv extends Television {
  
    // the LcdTv subclass has one field
    public String contrastRatio;

    // the LcdTv subclass has one constructor
    public LcdTv(int tvSize, String tvColorType, String tvContrastRatio) {   

        super(tvSize, tvColorType);
        contrastRatio = tvContrastRatio;
    }  

    // the LcdTv subclass has one method
    public void setHeight(String newValue) {

        contrastRatio = newValue;
    }  

}


Sunday, January 2, 2011

JAVA : Version Checker

JAVA Version Checker

What Version of Java is Running on your System?

To Know answer that Java Code Spot has wrote a simple Java Version checker Application that tells what version of Java is currently running on your system. Your system has the information about the version of Java installed and you can get that inforamtion for Java Runtime Enviornment of your system. Version checker Applet also displays some other information like Vendor of Java, Operating system, OS Version and OS Architecture.


!!!!!!!!!!....Applet may take some time to load....!!!!!!!!!!

If You have Java installed and enabled in your browser you will see a pink color rectangle above with information displaying. If you don't see a pink rectangle it means Java may not be installed.

In this application we are getting information about Java and Operation System by a simple code
System.getProperty(String Key);

This method is used to get System properties according to parameter given in String.


Java Code to get System Information:

                         System.getProperty("java.version");
                         System.getProperty("java.vendor");
                         System.getProperty("os.name");
                         System.getProperty("os.version");
                         System.getProperty("os.arch");
 

You can also get some other information using this method

S/No.   Key                                               Description
---------------------------------------------------------------------------------------------------------
1        java.version                                   The version of Java Runtime Environment.
2        java.vendor                                   The name of Java Runtime Environment vendor
3        java.vendor.url                              The URL of Java vendor
4        java.home                                      The directory of Java installation
      user.name                                      The name of account name user
6        user.home                                      The home directory of user
7        user.dir                                          The current working directory of the user
8        java.vm.version                             JVM implementation version
9        java.vm.vendor                              JVM implementation vendor
10      java.vm.name                                JVM  implementation name
11      java.specification.version               The name of specification version Java Runtime Environment
12      java.specification.vendor                JRE specification vendor
13      java.class.version                           Java class format version number
14      java.class.path                               Path of java class
15      java.library.path                             List of paths to search when loading libraries
16      java.io.tmpdir                                The path of temp file
17      java.compiler                                 The Name of JIT compiler to use
18      os.name                                       The name of OS name
19      os.arch                                         The OS architecture
20      os.version                                    The version of OS

Download Files Here: