Please explain to me the simple language, the actions in the code.
-
public class Main { public static void main(String[] args) { Cat cat = new Cat(); System.out.println(cat.name); changeName(cat); System.out.println(cat.name); } public static void changeName(Cat cat) { cat.name = "Jerry"; } public static class Cat { String name = "Tom"; } }
-
starting with method.
main
, this is the entry point at which the programme begins.public static void main(String[] args) {
Then create a class object
Cat
by namecat
Cat cat = new Cat();
Look at the class.
public static class Cat { String name = "Tom"; }
Class has one characteristic, a string field under the name
name
in which the value is immediately recorded"Tom"
♪We're going to put the field in the console.
name
Previous class objectCat
System.out.println(cat.name);
caused by a static method which, in the parameters, adopts any class object
Сat
and change the meaning of the fieldname
♪"Jerry"
public static void changeName(Cat cat) { cat.name = "Jerry"; }
And at the end, we'll get the new meaning of the field.
name
facilitycat
System.out.println(cat.name);