0% found this document useful (0 votes)
39 views5 pages

Getter_Setter_this

The document explains the concepts of encapsulation, getters, setters, and the 'this' keyword in Java, which are essential components of Object-Oriented Programming. It emphasizes how encapsulation protects data by using getter and setter methods for controlled access to private variables. Additionally, it illustrates the use of the 'this' keyword to refer to the current instance of a class and avoid naming conflicts.

Uploaded by

Bhargavi
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)
39 views5 pages

Getter_Setter_this

The document explains the concepts of encapsulation, getters, setters, and the 'this' keyword in Java, which are essential components of Object-Oriented Programming. It emphasizes how encapsulation protects data by using getter and setter methods for controlled access to private variables. Additionally, it illustrates the use of the 'this' keyword to refer to the current instance of a class and avoid naming conflicts.

Uploaded by

Bhargavi
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/ 5

Understanding Getter, Setter, and this Keyword in Java

A Guide to Encapsulation and Object-Oriented Programming


Introduction:
•Java follows the Object-Oriented Programming (OOP) paradigm.
•Encapsulation is a key OOP principle that helps protect data.
•Getters and Setters provide controlled access to private variables.
•this keyword helps refer to the current instance of a class.
Encapsulation in Java:
•Encapsulation means hiding data within a class.
•Data is accessed using getter and setter methods.
•Helps in data security and integrity.
What are Getters and Setters?
•Getter: Retrieves the value of a private variable.
•Setter: Modifies the value of a private variable.
•Helps implement data hiding.
Getter
A method that retrieves (or "gets") the value of a private field. Typically starts with get followed by the
field name.
Setter
A method that updates (or "sets") the value of a private field.Typically starts with set followed by the
field name.

Example of Getters and Setters:


class Car {
private String model;
// Setter method
public void setModel(String model) {
this.model = model;
} Explanation of the Code
// Getter method •private String model; → Private variable
public String getModel() { •setModel(String model) → Setter method to assign
return model; value
•getModel() → Getter method to retrieve value
}
•Uses this keyword to refer to instance variable.
}
The this Keyword:
•this refers to the current instance of a class.
•Avoids confusion when variable names are the same.
•Helps in constructor chaining and returning the current object.
Example of this Keyword

class Student {
private String name;
// Constructor using 'this’
public Student(String name) {
this.name = name;
}
// Method using 'this’
public void display() {
System.out.println("Student Name: " + this.name);
}
}
Example-2:
public class Person {
// Private fields
private String name;
// Getter for name
public String getName() {
return name;
}
// Setter for name
public void setName(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person();
// Setting values using setters
person.setName("Alice");
// Getting values using getters
System.out.println("Name: " + person.getName());
}

You might also like