Wednesday, December 17, 2008

What is a Class

Welcome back to Java Code Online. Class is the most commonly thing in Java. Having a good understanding of class is essential for getting a firm grip on the Principle of OOPS and Java itself. A class is called the blueprint of an Object. Everything that is done in Java, has to be enclosed within a class.

As I said before that the class is a blueprint of an Object, it is used for making Objects. I Java we know that everything is an Object, and if we don't know then we must know this. Java is built on the Principle of OOPS and is totally Object Oriented. So Java see everything as an Object.

I will take you through a through understanding of this concept. A class is used to define a new data type, and normally a separate class is used to make Objects of this new data type. Think of a class as something which defines what an Object knows and what an Object does. Therefore a class is also called a template for an Object and an Object is called an instance of the class. Thus a class contains the instance variables (things which an Object knows) and methods (things which an Object does).

A class is just like a blueprint and it covers every intricate detail about the Object that will be made from it. The normal structure of class is that it defines everything about an Object, so suppose that your application consists of some Objects, and so you make classes for each. Now you define a main class which will initiate all the calling procedure.

The main class always contain the method "main". This method tell the JVM that this is the main class and it need to start execution from here. Once in the main class, you can define Objects of other classes that you have made. I will take a simple example to clarify my point.

// This program declares two Car objects.
class Square {
int width;
int height;

class SquareDemo2 {
public static void main(String args[]) {
Square mySquare1 = new Square();
Square mySquare2 = new Square();
int area;
// assign values to mySquare1's instance variables
mySquare1.width = 10;
mySquare1.height = 20;
/* assign different values to mySquare2's instance variables */
mySquare2.width = 15;
mySquare2.height = 15;
// compute area of first Square
area = mySquare1.width * mySquare1.height;
System.out.println("Area is " + area);
// compute area of second box
area = mySquare2.width * mySquare2.height;
System.out.println("Area is " + area);

The first System.out.println results in:- 200
The second System.out.println results in:- 225

Now you must be wondering about so many new things, I have shown above. I will cover each of them.

Here we have defined two classes Square and SquareDemo2. The first class is used for making an Object of type Car. The second Class uses this new data type for making new Objects. Now as told before, the execution begins from the class containing the "main" method.

Now as you can see that there are two different outputs for the two Objects, this shows that both the Objects are unique and are independent of each other. Here System.out.println is used for displaying the results at the console or output screen.

The lines starting with // are skipped by the Java compiler. Infact the // is the syntax for Comment in Java. These are very helpful in explaining the functioning of the code.

If you have any problems with the code that I have explained right now, then do leave me a comment and I will try my best to clarify your doubt.

The most important thing that you must have noticed here is the parameter "new". This is the magic keyword of Java and is used for creating new Objects. I will discuss more on creating a new Object in my later posts.

I hope the article was helpful in explaining you about What is a Class. If you like this article then do leave a comment. For more information on Java, keep buzzing Java Code Online.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.