Here is a simple implementation of Getters and Setters in Java. The code is very short and easy to understand. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Program Listing
Student.java
package list;
public class Student {
private String name;
private int age;
private int a,b;
public String getName(){
return name;
}
public int getAge()
{
return age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age) {
this.age =age;
}
public int get_A()
{
return a;
}
public int get_B()
{
return b;
}
public void Set_A(int a){
this.a = a;
}
public void Set_B(int b){
this.b = b;
}
}
Test.java
package list;
public class Test {
public static void main(String[] args){
int sum=0;
Student s=new Student();
s.setName("Mario");
s.setAge(38);
s.Set_A(1);
s.Set_B(4);
sum = s.get_A() + s.get_B();
System.out.println("Your Name is " + s.getName());
System.out.println("Your Age is " + s.getAge());
System.out.println();
System.out.print("The sum of " +s.get_A() + " and " + s.get_B() + " is " + sum + ".");
}
}
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Program Listing
Student.java
package list;
public class Student {
private String name;
private int age;
private int a,b;
public String getName(){
return name;
}
public int getAge()
{
return age;
}
public void setName(String name){
this.name=name;
}
public void setAge(int age) {
this.age =age;
}
public int get_A()
{
return a;
}
public int get_B()
{
return b;
}
public void Set_A(int a){
this.a = a;
}
public void Set_B(int b){
this.b = b;
}
}
Test.java
package list;
public class Test {
public static void main(String[] args){
int sum=0;
Student s=new Student();
s.setName("Mario");
s.setAge(38);
s.Set_A(1);
s.Set_B(4);
sum = s.get_A() + s.get_B();
System.out.println("Your Name is " + s.getName());
System.out.println("Your Age is " + s.getAge());
System.out.println();
System.out.print("The sum of " +s.get_A() + " and " + s.get_B() + " is " + sum + ".");
}
}