Classes And Object
A class is a model for creating object.This means the properties and action of the object arewritten in the class. Properties are represented by variable and action of the objectare represent by methods. So class a contains variables and mehtods.
If we take a person classes . we can write code in the class that specifies the properties and
action performed by any person. For example ......
A person has properties like name, age, etc. Similarly, a person can perform an action like
talking, walking. etc. So the class Dog contains these properties and action shows here
class Dog
{
// properties - instance variable
String name="tiger";
int age=12;
// action -methods
void talk(){
System.out.println("hello Iam "+name);
System.out.println("MY age Is "+age);}
}
class MainClass
{
public static void main (String[]args)
{
Dog d=new Dog();
d.talk();
}
}
Object Creation
we know that the class code along with method code is stored in method area of the JVM .
when an object is created. the memory is allocated on "Heap"ofter creation of an objectJVM produces unique reference number for the object from the memory address of the object.
This is reference number is also called hash code number. Ex...................
hash code number and reference of an object
Employee e1=new Employee();// e1 is reference of employee object
System.out.println(e1.hashCode());// display hash code stored in e1.
0 comments:
Post a Comment