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

Classand Objects

This document provides an overview of classes and objects in Java, explaining that a class serves as a template for creating objects, which are instances of the class. It covers key concepts such as instance and static members, access modifiers, and the use of getter and setter methods for encapsulation. Additionally, it discusses parameter passing and the nature of arrays in Java as objects that can hold both primitive and object references.

Uploaded by

ommprasadjena10
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 views7 pages

Classand Objects

This document provides an overview of classes and objects in Java, explaining that a class serves as a template for creating objects, which are instances of the class. It covers key concepts such as instance and static members, access modifiers, and the use of getter and setter methods for encapsulation. Additionally, it discusses parameter passing and the nature of arrays in Java as objects that can hold both primitive and object references.

Uploaded by

ommprasadjena10
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

Chapter Summary Class and

Objects
1. A class is a template for objects. It defines the properties of objects and provides
constructors for creating objects and methods for manipulating them.
2. A class is also a data type. You can use it to declare object reference variables. An object
reference variable that appears to hold an object actually contains a reference to that object.
Strictly speaking, an object reference variable and an object are different, but most of the
time the distinction can be ignored.
3. An object is an instance of a class. You use the new operator to create an object, and the
dot operator (.) to access members of that object through its reference variable.

4. An instance variable or method belongs to an instance of a class. Its use is associated with
individual instances. A static variable is a variable shared by all instances of the same class.
A static method is a method that can be invoked without using instances.
5. Every instance of a class can access the class’s static variables and methods. For clarity,
however, it is better to invoke static variables and methods using [Link]
and [Link].

6. Modifiers specify how the class, method, and data are accessed. A public class, method, or
data is accessible to all clients. A private method or data is accessible only inside the class.
7. You can provide a get method or a set method to enable clients to see or modify the data.
Colloquially, a get method is referred to as a getter (or accessor), and a set method as a
setter (or mutator).
8. A get method has the signature
public returnType getPropertyName().
If the returnType is boolean, the get method should be defined as
public boolean isPropertyName().
A set method has the signature
public void setPropertyName(dataType propertyValue).

9. All parameters are passed to methods using pass-by-value. For a parameter of a primitive
type, the actual value is passed; for a parameter of a reference type, the reference for the
object is passed.
10.A Java array is an object that can contain primitive type values or object type values.
When an array of objects is created, its elements are assigned the default value of null.
Little Elaboration of the above topics

Classes and Objects – Key Concepts


1. Class
A class is a template or blueprint used to create objects.
It defines:
• Properties (variables / fields) of objects
• Methods (functions) to manipulate the data
• Constructors used to create objects

Example
class Student {

int rollNo;

String name;

double marks;

void display() {

[Link](rollNo + " " + name + " " + marks);

Here:
• rollNo, name, marks → properties

• display() → method

2. Class as a Data Type


A class can also act as a data type.
It allows us to declare object reference variables.
Important distinction:

Term Meaning
Object Actual instance created in memory
Reference variable Variable that stores the address of the object
Example
Student s;

Here:
• Student → data type

• s → reference variable

Object creation:
s = new Student();

Now s refers to a Student object.

3. Object
An object is an instance of a class.
Objects are created using the new operator.
Members of the object are accessed using the dot (.) operator.

Example
Student s = new Student();

[Link] = 101;

[Link] = "Rahul";

[Link] = 88.5;

[Link]();

4. Instance vs Static Members


Instance Variable / Method
Belongs to each object separately.
Example:
class Student {

int rollNo; // instance variable

Each object has its own rollNo.

Static Variable
Shared by all objects of the class.
Example
class Student {

static String college = "ABC College";


}

All students share the same college.

Static Method
A static method can be called without creating an object.
Example
class MathUtil {

static int square(int x) {

return x * x;

public class Test {

public static void main(String[] args) {

[Link]([Link](5));

5. Accessing Static Members


Static members should ideally be accessed using the class name.

Recommended
[Link]

[Link](5)

Not Recommended
[Link]

6. Access Modifiers
Modifiers control accessibility.

Modifier Access
public accessible everywhere
private accessible only inside the class
Example
class Student {

private int marks;


public void setMarks(int m) {

marks = m;

public int getMarks() {

return marks;

7. Getter and Setter Methods


To implement encapsulation, data members are often kept private.
Access is provided using:
• Getter → read value
• Setter → modify value

Example
class Student {

private int marks;

public int getMarks() {

return marks;

public void setMarks(int m) {

marks = m;

8. Naming Convention for Getters and Setters


Getter
General form:
public returnType getPropertyName()

Example
public int getAge()

For boolean values


public boolean isPropertyName()

Example
public boolean isActive()

Setter
General form
public void setPropertyName(dataType value)

Example
public void setAge(int age)

9. Parameter Passing in Java


Java uses pass-by-value.
Two cases:

Primitive Type
Actual value is passed.
Example
void change(int x) {

x = 10;

Original value does not change.

Reference Type
The reference (address) is passed by value.
Example
class Test {

int x;

void change(Test t) {

t.x = 50;

}
The object's data can change.

10. Arrays in Java


A Java array is an object.
It can store:
• primitive values
• object references

Example: Primitive Array


int[] a = new int[5];

Example: Object Array


Student[] s = new Student[3];

Initially
s[0] = null

s[1] = null

s[2] = null

Objects must be created separately.


s[0] = new Student();

s[1] = new Student();

s[2] = new Student();

You might also like