============Main Class=================================
public class MainClass {
public static void main(String[] args){
Person person1 = new Person("Andrew");
Person person2 = new Person();
person1.setName("Alex");
}
}
============Person Class===============================
public class Person {
//instance variable
private String name;
//constructor to set name to argument given in parameter
public Person(){
this("default name");
}
//constructor to set name to argument given in parameter
public Person(String name){
this.name = name;
System.out.println("Person created: " + name);
}
//setter method to set name variable of object, displays name change
public void setName(String name){
this.name = name;
System.out.println("Changed name to " + getName(this));
}
//getter method to return object name, takes object in parameter
public String getName(Person person){
return name;
}
}
On this page of the site you can watch the video online Java Programming Tutorial - 20 - Using "this" in Java with a duration of hours minute second in good quality, which was uploaded by the user Andrew Jones 05 November 2017, share the link with friends and acquaintances, this video has already been watched 3,572 times on youtube and it was liked by 50 viewers. Enjoy your viewing!