2.3 OOPS-Memory in Java.
Object -> Any entity which has small state and behaviour is known as object.
The state represent the characteristics of object where as behaviour represent the functionality of the object.
Class -> class is a defination block which define the object properties .
The non-static data members of the class defines states of the object where as the non-static members function define behaviour of the object.
A class can be define only one type of object from a class we can create any number of instance help of new operator.
Example :
class Pen
{
// data members
String inkColor;
String brandName;
double price;
// member function
void write()
{
System.out.println("writting......!");
}
void refill()
{
System.out.println("refilling......!");
}
void throwPen()
{
System.out.println("throwing......!");
}
}
class MainClass2
{
public static void main(String args[])
{
System.out.println("program started");
Pen p1 = new Pen();
p1.inkColor = "blue";
p1.brandName = "cello";
p1.price = 15.63;
p1.write();
p1.refill();
p1.throwPen();
System.out.println("program ended");
}
}
Example Program2 :
class NoteBook
{
int pages;
String brandName;
double price;
void open()
{
System.out.println("Opening book.....!");
}
void close()
{
System.out.println("Closing book....!");
}
void trunPage()
{
System.out.println("trun to next page.......!");
}
}
class MainClass3
{
public static void main(String args[])
{
System.out.println("program started");
NoteBook b1 = new NoteBook();
b1.pages = 100;
b1.brandName = "classmate";
b1.price = 35.25;
b1.open();
b1.close();
b1.trunPage();
System.out.println("Program Ended");
}
}
Memory in Java :
.class
1.class loading. |
2. Initialization. |
3. Execution. |
ClassLoader : used to load class into jvm.
There are four-memory Area :
1. Heap Area : object of class is store here.
2. Static pool-area : Static member of class is store here.
3. Method Area : userd to store method defination/body.
4. Stack Area : Stack Area used for execution LIFO, local variable are store here.
Initialization Block : Java provide two type of block to initialize the data member of a block.
1. Static Initialization Block: used to initialize the static data member of the class.
-> static block are executed at the time of class loading.
-> per class it will be executed only once.
-> In a class we can defined multiple static blocks , all the static block are execute sequential.
-> if a class contains both main method and static block JVM first execute static block than start the execution of main method.
2. Non-Static Initialization Block : are used to initialize the non-static data member of the class.
-> non-static block are executed at the time of instanciation.
-> for each instanciation the non-static block are executed.
-> if a class is having static and non-static block than first static block will be executed than the non-static block.
Comments
Post a Comment