0% found this document useful (0 votes)
2 views6 pages

Getter and Setter in Java

The document explains the concept of getters and setters in Java, highlighting their role in accessing private variables through public methods. It provides examples of class implementations, including a 'Person' class with name management and a 'student' class that includes validation for roll numbers. Additionally, it demonstrates the use of constructors and methods to display book details in a 'book_dem' class.

Uploaded by

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

Getter and Setter in Java

The document explains the concept of getters and setters in Java, highlighting their role in accessing private variables through public methods. It provides examples of class implementations, including a 'Person' class with name management and a 'student' class that includes validation for roll numbers. Additionally, it demonstrates the use of constructors and methods to display book details in a 'book_dem' class.

Uploaded by

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

Getter and setter in

Java
Get and Set

• You learned that private variables can only be accessed within the
same class (an outside class has no access to it). However, it is
possible to access them if we provide public get and set methods.

• The get method returns the variable value, and the set method sets
the value.

• Syntax for both is that they start with either get or set, followed by
the name of the variable, with the first letter in upper case:
Example
public class Person { public class Main {
private String name; // private = restricted access public static void main(String[] args) {
Person myObj = new Person();
public String getName() {
[Link]("John"); // Set the value
return name;
of the name variable to "John"
}
[Link]([Link]());
public void setName(String newName) { }
[Link] = newName; }
}
}

• The get method returns the value of the variable name.


• The set method takes a parameter (newName) and assigns it to the name variable.
• ‘this’ is a reference variable that refers to the current object.
• the name variable is declared as private, we cannot access it from outside this class:
• A return statement causes the program control to transfer back to the caller of a method
• The get method returns the value of the variable name.
• The set method takes a parameter (newName) and assigns it to the
name variable. The this keyword is used to refer to the current object.
• the name variable is declared as private, we cannot access it from
outside this class:
Create a class student with data members name, rollno and write a method to display the value of name and
[Link] getter and setter methods for rollno, if rollno is more than 10 it will display “invalid data” otherwise
it will display rollno.

package session_22_23;
public class student { Main function
private String name;
private int rollno;
package session_22_23;
public int getRollno() public class main_fun {
{ public static void main(String[] args)
return [Link];
}
{
public void setRollno(int rollno) student st1=new student();
{ if (rollno>=10) [Link]();
{
throw new IllegalArgumentException("invalid data");
[Link](12);
} [Link]();
[Link]=rollno; }
}
public void disp()
{ [Link]([Link]); }
[Link](rollno);
}}
Example of class design, default constructor,
parameterized constructor, setter -getter
public class book_dem {
private String title;
private String author;
Main function
private String publisher; public class check {
private String genre;
private double price;
void display() {
[Link]("book details");
public static void main(String[] args) {
[Link]("Title:"+title); book_dem book1=new book_dem();
[Link]("author:"+author);
[Link]("Publisher:"+publisher); [Link]();
[Link]("Genre:"+genre);
[Link]("Price:"+price);
book_dem book2=new
} book_dem("java","xyz","penguin","text
book_dem() {
title=""; book",240.30);
author=""; [Link]();
publisher="";
genre=""; [Link]("book2
price=0.0;
}
price:"+[Link]());
book_dem(String t,String a,String pub,String g,double pr) { [Link]([Link]()+50);
title=t;
author=a; [Link]("book2
publisher=pub;
genre=g;
price:"+[Link]());
price=pr; }
}
double getprice() { }
return price;
}
void setprice(double pr) {
price=pr;
}

You might also like