0% found this document useful (0 votes)
6 views1 page

Geters&seters

The document explains the concepts of getters and setters in programming, which are methods used to retrieve and update private fields, respectively. It provides an example of a 'Person' class with a private field 'name' and demonstrates the implementation of a getter and setter for that field. The 'Main' class shows how to create an instance of 'Person', set the name, and retrieve it using the defined methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Geters&seters

The document explains the concepts of getters and setters in programming, which are methods used to retrieve and update private fields, respectively. It provides an example of a 'Person' class with a private field 'name' and demonstrates the implementation of a getter and setter for that field. The 'Main' class shows how to create an instance of 'Person', set the name, and retrieve it using the defined methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Getters:

Methods that retrieve (or "get") the value of a private field.


Typically prefixed with get followed by the field name in camel case.

Setters:
Methods that update (or "set") the value of a private field.
Usually prefixed with set followed by the field name in camel case.

class Person {
private String name; // Private field

// Getter method
public String getName() {
return name;
}

// Setter method
public void setName(String name) {
this.name = name;
}
}

public class Main {


public static void main(String[] args) {
Person p = new Person();
p.setName("Alice"); // Setting the name
System.out.println(p.getName()); // Getting the name
}
}

You might also like