![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7bd3U3RpPwjHhqhnYGi-pBcje0EK3nvwponBSd1IzctIfrAwe22MLm8jwpvx5ZrhKi_oClw6Y6GC1UvQfnalhFXPN7Lof4a-qMYJeO3nG4RX0UEgjnbo8uniMUz8RYy8tfQQMFvPW68hT/s200/sun-java.jpg)
A class is declared with the help of the "class" keyword. A simple definition of a class is shown here.
class classname{
type instance-variable1;
type instance-variable2;
//...
type instance-variableN;
type methodname1(parameter-list){
//body of method
}
type methodname2(parameter-list){
//body of method
}
//...
type methodname3(parameter-list){
//body of method
}
}//end of class.
The data, or variables defined in a class are the instance variables.They are called instance variable because each instance of that class i.e object contains its own copy of these variables.Thus , the data for one instance is separate and unique from the data from other . The code is within the block of the method.Collectively these methods and variables are known as the member of the class.
DECLARING OBJECTS
Obtaining an object is a two step process. First we have to declare a variable of that class. This variable doesnot define an object, it actually refers to an object. In second step , we have to create a physical copy of the object and assign it to that variable. We do this with the help of "new operator". This new operator help us to allocate memory at run time for an object and returns a reference to it.This reference is more or less the address in the memory of the object allocated by the new operator.This reference is then stored in the variable.Thus in java all class objects are "{dynamically allocated".
Declaration :
Box mybox; //declare reference to object
mybox = new Box(); //allocate a box object
Here, Box is the class and mybox is the variable.In the first step , we are creating a reference to an object of type Box.After the execution of first step, mybox contains null , which indicates that it dose not point to an object .Any attempt to access this at this point will give a compile time error.In second step, we are giving it a physical existence by allocating memory with the help of new operator.In reality the mybox simply holds the memory address of the actual Box object.
The above two statements can be combined together
Box mybox = new Box();
In java an object reference is similar to a memory pointer but we cannot manipulate this references like we did in actual pointers.
Assigning Object Reference Variables
Box b1 = new Box();
Box b2 = b1;
In the above statements , b1 and b2 both refer to the same object.The assignment of b1 to b2 dose not allocate any memory space or copy any part of the original object as does b1.Thus , any changes made in b2 will affect to which b1 is referring , since they are the same object.Although b1 and b2 refers to the same object but they are not linked.For example :
Box b1 = new Box();
Box b2 = b1;
//..
b1 = null;
Here, b1 has been set to null but b2 still points the original object..
New Operator
A new operator helps to allocate memory at run time.Since there is a finite memory available to us.It may happen that there is insufficient memory space available, so at the run time the new operator cannot allocate memory. This will generate a run time exception.(Exception are the run time error generated due to erroneous input ,memory problem etc.)
Introduction Methods
Methods are nothing but the block of code defined within a class.A class without any methods doesnot have any life.It consist of the code block which executes to give a desired output.
The declaration of a Method:
type methodname(parameter-list){
//body of method
}
a method declaration has 4 parts :
1. type - the return type of the method like int , void etc.
2. methodname- it is the valid identifier
3.parameterlist - it is the number of parameters i.e the variables defined by the method that receives a value when method is called.(note- arguments are the values that are passed to a method when it is called)
4.body of method : it is the block which contains the code of the method.
No comments:
Post a Comment