0% found this document useful (0 votes)
6 views4 pages

8 ClassAndObject

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, focusing on classes and objects. It explains the definition of a class, its components such as fields and methods, and the process of creating an object using reference variables and constructors. Several code examples illustrate the creation and manipulation of objects in Java.

Uploaded by

k.narendhar16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

8 ClassAndObject

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, focusing on classes and objects. It explains the definition of a class, its components such as fields and methods, and the process of creating an object using reference variables and constructors. Several code examples illustrate the creation and manipulation of objects in Java.

Uploaded by

k.narendhar16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SSSIT Computer Education

Besides R.S Brothers Show Room


Kphb – Hyderabad : 9866144861

Java

Working with OOPS :

Class and Object :


Class:
1.class 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

4.Default modifier for the class is “default”

5.the class member can be initialized

6.Class 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


1.Referenc variable
2.new and constructor

Syn for Reference Variable :


<ClassName > <ref.variablename>;
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: <ref.vname> = new <ClassName>([list of args]);
Eg: s=new Student( ); constructor

1.Constructor is an initializer method, which is used to instantiation and initialization


2.to invoke the constructor we must required “new” keyword
3.Whenever constructor is invoked then only an Object of the class is instantiated in the
heap in memory
4.Whatever 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( );
System.out.println(s);

Sample s2=new Sample();


System.out.println(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;
// System.out.println(x);
System.out.println("x val is : "+t.x);
}
}

//Testing.java
class Testing
{
int x;

public static void main(String args[])


{
Testing t=new Testing();
t.x=111;
System.out.println("x val is : "+t.x);
}
}

4|Page

You might also like