SSSIT Computer Education
Besides R.S Brothers Show Room
Kphb – Hyderabad : 9866144861
Java
Working with OOPS :
Class and Object :
Class:
[Link] is a keyword, By using class keyword , we defined our own data type is called user
defined data type
2. In the class we can declare a variable called fields , which are used to store the data
3. We can also define functions called methods, which are used to manipulate the values
[Link] modifier for the class is “default”
[Link] class member can be initialized
[Link] is a template or class is a collection of states and behavior
Syn: [modifiers] <class > <ClassName>
{
Fields
Methods
}[;]
Eg: class Student
{
int sno;
String sname;
int stot;
}
Object:
> As we know that class is a logical representation, it doesn’t occupy any space to store
the data, To store the data and to access members of the class then we required to create
a physical representation of the class called an “Object”
1|Page
SSSIT Computer Education
Besides R.S Brothers Show Room
Kphb – Hyderabad : 9866144861
Java
> In simple words an object is an instance of a class
Note : To Create an Object of the class then we must required
[Link] variable
[Link] and constructor
Syn for Reference Variable :
<ClassName > <[Link]>;
Eg: Student s;
1. For Reference variable memory is allocated with in the stack
2. It will have two partitions i.e type and hcode , Type is holding class type name where as
hashcode is holding an identity of an object.
Syn For Object Creation:
Syn: <[Link]> = new <ClassName>([list of args]);
Eg: s=new Student( ); constructor
[Link] is an initializer method, which is used to instantiation and initialization
[Link] invoke the constructor we must required “new” keyword
[Link] constructor is invoked then only an Object of the class is instantiated in the
heap in memory
[Link] the non static variable are existed in the class for those variable memory is
allocated with in the object.
2|Page
SSSIT Computer Education
Besides R.S Brothers Show Room
Kphb – Hyderabad : 9866144861
Java
Image :
Ex1:
class Sample
{
}
class ObjectDemo
{
public static void main(String args[])
{
Sample s; // s is reference variable
s=new Sample( );
[Link](s);
Sample s2=new Sample();
[Link](s2);
}
}
Eg:
3|Page
SSSIT Computer Education
Besides R.S Brothers Show Room
Kphb – Hyderabad : 9866144861
Java
class Test
{
int x;
}
class ObjectDemo2
{
public static void main(String args[ ])
{
Test t=new Test( );
t.x=10;
// [Link](x);
[Link]("x val is : "+t.x);
}
}
//[Link]
class Testing
{
int x;
public static void main(String args[])
{
Testing t=new Testing();
t.x=111;
[Link]("x val is : "+t.x);
}
}
4|Page