Class Xii
Class Xii
Object:- Once the class has been defined we create the variables of that
class using declaration this variable is instance or object of the class.
}
Fields Declaration
Class Rectangle
{
int length;
int width;
}
These variables are only declared and therefore not staroge space has been created in the
memory.
Method declaration
type mehodname (parameter list)
{
method body
}
Example:
Class Rectangle
{
int length, width;
void getdata(int x ,int y)
{
length=x;
width=y;
}
int rectArea() // declaration of another method
{
int area=length *width;
return (area);
}
}
Wrong way to declare a method
void getdata(int x, y) // Incorrect
Scope of Variables
Class Access
{
int x;
void method1( )
{
int y;
y=10; //legal
y=x; // legal
}
void method 2( )
{
int z;
x=5; // legal
z=10; // legal
y=1; //illegal
}
}
Creating Objects: Object in java are created using the new operator.
Rectangle rect1; // declare the object
rect1=new Rectangle( ); // instantiate the object
Both statement can be combined into one as below
Rectangle rect1= new Rectangle ( );
Rectangle rect2= new Rectangle ();
Rect1.getdata(55, 12);
Program:1
class Rectangle
{
int length ,width;
void getdata(int x , int y)
{
length =x;
width = y;
}
int rectArea( )
{
int area;
area =length * width;
return (area);
}
}
Class RectArea
{
public static void main(String args[])
{
int area1,area2;
Rectangle rect1=new Rectangle() ; // creating objects
Rectangle rect2=new Rectangle();
rect1.length=15;
rect1.width=10;
area1=rect1.length*rect1*width;
rect2.getdata(20,12); //Accessing method
area2=rect2.rectArea();
System.out.println(“Area 1=“+ area1);
System.out.println(“Area2=“+ area2);
}
}
Constructor:- There are two way to initialize all the variable of class.
First Way:-
rect1.length=15;
rect1.width=10;
Second Way:-
rect1.getdata(20,12);
Another Way:- Java supports a special type of method ,called
constructor ,that enables an object to initialize itself when it is created.
Constructor have the same name as the class itself ,Secondly they do not
specify a return type ,not even void .
Example:-
class Rectangle
{
int length;
int width;
Rectangle(int x, int y) // constructor method
{
length=x;
width=y;
}
int rectArea( )
{
return (length*width);
}
}
Class RectangleArea
{
int area1=rect1.rectArea();
System.out.println(“Area1=“+ area1+);
Instance Members:- The instance methods are those that are defined without the
keyword static .the dot ‘.’ notation with a object reference is used to access instance
methods.
Static Members: The static members are those that are declared with keyword
static.Static members can be accessed either by using the class name or by using the
object reference ,but instance members can only be accessed via object references.
Example:
Class Mathoperation
{
static float mul(float x,float y)
{
return (x*y);
}
public float divide(float x ,float y)
{
return (x*y);
}
}
class MathApplication
{
public void static main(String args[] )
{
float a=Mathoperation.mul(4.0 ,5.0 );
Mathoperation obj1=new Mathoperation();
float b=obj1.divide(6.0 , 2.0);
System.Out.println(“a =“+ a);
System.Out.Println(“b =“ +b);
}
}