Tuesday, January 19, 2016

Topic 10: Class in Java

  • A class defines a new data type

  • Class type can be used to create objects. Thus, a class is a template for an object, and an object is an instance of a class.

  • A class is declared by use of the class keyword. The data, or variables, defined within a class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.

  • Each instance(or object) of the class contains its own copy of instance variables.

  • To access instance variables, we will use the dot (.) operator.

 

Consider the following example which defines a class Box:

/* Call this file BoxDemo.java */
class Box
{
double width, height, depth;
}

class BoxDemo
{
public static void main(String args[])

{
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;

// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

When we compile this program, we will find two .class files created, one each for Box and BoxDemo. The Java compiler automatically puts each class into its own .class file

If we have two Box objects, each has its own copy of depth, width, and height. It is important to understand that changes to the instance variables of one object have no effect on the instance variables of another.


Declaring Objects

Obtaining objects of a class is a two-step process:

  • Declare a variable of the class type.

  • Acquire an actual, physical copy of the object and assign it to that variable using the new operator. The new operator dynamically allocates memory for an object and returns a reference to it. This reference is, more or less, the address in memory of the object allocated by new. This reference is then stored in the variable.

For ex:

Box mybox; // declare variable of Box class type 
mybox = new Box(); // allocate a Box object


The new operator dynamically allocates memory for an object
. It has this general form:

class-var = new classname( );

Here, class-var is a variable of the class type being created. The classname is the name of the class that is being instantiated. The class name followed by parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created. Most real-world classes explicitly define their own constructors within their class definition. However, if no explicit constructor is specified, then Java will automatically supply a default constructor. 


Assigning Object Reference Variables

Consider the following code assignment statements:

Box b1 = new Box();
Box b2 = b1;

  • After this fragment executes, b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as does b1. Thus, any changes made to the object through b2 will affect the object to which b1 is referring, since they are the same object.

  • Both b1 and b2 are independent of one another. For example, a subsequent assignment to b1 will simply unhook b1 from the original object without affecting the object or affecting b2. For example:

Box b1 = new Box();
Box b2 = b1;
b1 = null;

Here, b1 has been set to null, but b2 still points to the original object.

Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.


No comments:

Post a Comment