Monday, October 3, 2011

Structure of a Java Application, Introduction to Classes and Object


Structure of a Java Application, Introduction to Classes and Object

In Java, you can either create a Stand-alone application or a networked application. a stand-alone application uses the resources of a local computer while a networked application uses resources available over a network.  Classes and object form the basis of the structure of a Java application. A Java application consists of a class, in which you declare the data members and methods. An object is an instance of a class and it encapsulates the method and data members of a class.
Creating Classes, Objects and Methods
All concepts that you need to implement in a Java application is encapsulated within a class. A class defines the attributes and methods of objects of the same type sharing common characteristics.


The main components of a class are:
Data members
Methods

Classes contain statements that include the declaration of data members, which specify the type of data to be stored.  Methods of a class contain a set of statements executable statements that gives a desired output. Methods define the action to be carried out on the data members of a class. Every class’s body is enclosed in a pair of left and right braces, {}, which indicate the start and end of the class.

Creating Classes in Java
Various data member and methods of a class are declared inside a class. The following syntax shows how to declare a class:
Class ClassName
{
//declaration of data members
//declaration of methods
}
Every class declaration begins with the Java keyword class, followed immediately by the class’s name which is required to create objects of the class.
Below is a code snippet to declare the Ginowine class that defines various data members, such as age, sex, and hair color.
Class Ginowine
{
     Int age;
     String sex;
     String hairColor;
}

Creating Objects of Classes
Remember an object is an instance of class, so before you can create an object in Java, you must have first declared a class. Classes and objects are closely linked to each other. To create an object, you need to perform the following steps:
  1. Declaration: declares a variable that holds the reference to the object. The following syntax shows how to declare an object of the class:
Class_name object_name
  1. Instantiation or creation: creates an object of the specified class. When you declare an object, memory is not allocated to it. Therefore, you cannot store data in the data members of the object. To allocate to the object, you need to use the new operator. The new operator allocates memory to an object and returns a reference to that memory location in the object variable. The following syntax shows how to create an object:
Class_name object_name = new class_name ();
To create an object of our class Ginowine, do the following
Ginowine gino = new Ginowine ();
Keyword new creates a new object of the class specified to the right of the keyword. The parentheses to the right of Ginowine are required. These parentheses in combination with a class name represent a call to a constructor, which is similar to a method, but is used only at the time of object creation to initialize the object’s data. In the above syntax, gino is now the object name and we can use this object (gino) to call methods of class Ginowine.

Accessing Data Members of a Class
Data members of an object are assigned values before they are being used. You can access the data members of a class outside the class by specifying the object name followed by the dot operator and the data member name. Example
Object_name.data_member_name
In the above syntax, object_name refers to the name of the object and data_member_name refers to the name of the data variable inside the object that you want to access.
You can use the following code snippet to access data members of Ginowine class, such as sex, age, haircolor and assign values to them through objects.
gino.age = 21;
gino.sex = ”male”;
gino.haircolor = ”black”;

Adding Methods to a Class
in a program, referring to multiple data members of a class can be a tedious task. In addition, accessing data members directly overrules the concept of encapsulation. You can create a method that can be used to access the data members. Using methods in a Java program provides the following advantages:
Reusability
Reduce Complexity
Data Hiding
The following syntax shows how to declare a method:
Void methodName ()
{
          // Method body.
}
In the preceding syntax, the void keyword specifies that the function does not return any value. The methodName specifies the name of the method.

Declaring method main ()

A Java program consist of the main () method that calls the methods defined in a class.  When writing an application, many classes are created. The Java compiler compiles all the classes in an application but to execute a program, you need to include the main () method. The following syntax shows how to declare the main () method:
Public static void main (String [] args)
{
     // codes for the main () method
}
In the preceding syntax, the method header contains three words public, static and void. The implications of these words are:
Public: The public keyword means that the method can be accessed from any object in a Java program.
Static: The static keyword is used with the main () method that associates the method with its class. You need not create an object of the class to call the main () method.
Void: The void keyword signifies that the main () method returns no value.
The main method can be declared in any class, but the name of the file and the class name in which the main () method is declared should be the same. The file must have the .java extension.  For example, if the main () method is declared in class Employee, the name of the file should be Employee.java.
The main () method accepts a single argument in the form of an array of elements of type String

Defining Constructor
you might have observed that to initialize the data members of a class, you need to assign values to each data member. However, it might turn out to be a tedious task if you need to initialize a large number of data members. In Java, you can create constructors of the classes that automatically initialize the data members of the class when you create an object. .

A constructor is a method with the same name as the class name. A constructor of a class is automatically invoked every time an instance of a class is created. Constructors do not have return type.

The following are the characteristics of a constructor:

A constructor has the same name as the class itself.
There is no return type for a constructor.
A constructor is used to assign values to the data members of each object created from a class.

No comments: