How to use setters, also known as set methods or mutators, in Java. In this video I also take a look at the ‘this’ keyword and when to use it.
When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.
Code for this tutorial:
class Frog {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setInfo(String name, int age) {
setName(name);
setAge(age);
}
}
public class App {
public static void main(String[] args) {
Frog frog1 = new Frog();
//frog1.name = "Bertie";
//frog1.age = 1;
frog1.setName("Bertie");
frog1.setAge(1);
System.out.println(frog1.getName());
}
}
Bertie
Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.
All pages on this site are copyright © 2013 John W. Purcell