+ 1

getter and setter example?

3rd Dec 2016, 5:24 AM
Nirmal Chandra Panda
Nirmal Chandra Panda - avatar
3 Réponses
+ 16
public class Person { private String name; private int age; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } } // down here an example for you to try the setters and getters public class PersonTest { public static void main(String[] args) { Person person1 = new Person(); Person person2 = new Person(); person1.setName("R2-D2"); person1.setAge(40); person2.setName("Yoda"); person2.setAge(900); System.out.println("person1 name is " + person1.getName()); System.out.println("person1 age is " + person1.getAge()); System.out.println("person2 name is " + person2.getName()); System.out.println("person2 age is " + person2.getAge()); } }
3rd Dec 2016, 6:03 AM
R2-D2
R2-D2 - avatar
0
thank you
3rd Dec 2016, 6:12 AM
Nirmal Chandra Panda
Nirmal Chandra Panda - avatar
0
0
3rd Dec 2016, 11:53 AM
Waheed Rehman
Waheed Rehman - avatar