Special Methods:
Getter and setter methods in Java are special methods used to access and update the private
Variables of a class. They provide a controlled way of accessing and modifying the variables,
Ensuring encapsulation.
1) Getter Method:
Getters are methods used to retrieve/get the value of a private variable. They are prefixed
with the word "get" followed by the variable name, with the first letter of the variable name
capitalized.
Syntax:
public VariableType getVariableName() {
return VariableName;
}
2) Setters Method:
Setters are methods used to update the value of a private field. They are typically prefixed
with the word "set" followed by the field name, with the first letter of the field name
capitalized.
Syntax:
public void setVariableName(VariableType VariableName) {
this.VariableName = VariableName;
Advantages of getter and setter methods:
➢ Read-Only or Write-Only Properties:
By providing only a getter or only a setter, you can make a property read-only or write-
only. This is useful in situations where you want to allow reading a value but not
modifying it, or vice versa.
➢ Encapsulation:
Encapsulation is a fundamental principle of object-oriented programming. By using
getters and setters, we can control access to the internal state of an object, hiding its
implementation details and only exposing a controlled interface.
pg.1
Program:
1a.
package specialmethods;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
1b.
package specialmethods;
public class MainPersonClass {
public static void main(String[] args) {
Person p1 = new Person();
p1.setName("John");
System.out.println(p1.getName());
}
}
o/p:
John
pg. 2
Scanner:
1. Scanner is a pre-defined class in java.util package.
2. Scanner class is used to accept dynamic input from the
User.
Rules to accept dynamic input from the user (or) Rules to
work around with Scanner class
1. Create an object of Scanner class.
2. Pass System.in to the Constructor call.
syntax: Scanner scan = new Scanner(System.in);
3. import Scanner class from java.util package.
syntax: import java.util.Scanner;
4. Make use of pre-defined methods to accept dynamic
input.
Important method used with respect to Scanner class
1. byte - nextByte()
2. short - nextShort()
3. int - nextInt()
4. long - nextLong()
5. float - nextFloat()
6. double - nextDouble()
7. boolean - nextBoolean()
8. String - next() or nextLine()
9. char - next().charAt(0)
pg. 3
Programs:
package dynamicread;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter the int type data");
int num1= sc.nextInt();
System.out.println(num1);
System.out.println("====================");
System.out.println("enter the double type data");
double num2= sc.nextDouble();
System.out.println(num2);
System.out.println("======================");
int[] a1 = new int[5];
for(int i=0 ; i<a1.length ; i++)
{
System.out.println("enter the int type data");
a1[i]=sc.nextInt();
for(int i=0 ; i<a1.length ; i++)
{
System.out.println(a1[i]);
}
}
pg. 4
o/p
enter the int type data
10
10
====================
enter the double type data
20.12
20.12
======================
enter the int type data
100
enter the int type data
200
enter the int type data
300
enter the int type data
400
enter the int type data
500
100
200
300
400
500
pg. 5