package en.codegym.task.jdk13.task04.task0403;
/*
What's the cat's name?
*/
public class Cat {
//this is an instance variable
private String name = "nameless cat";
public void setName(String name) {
this.name = name; //dont understand how is
// name variable goes to line 22 and reads "Charlie"
}
//this is a static method
// i read that You can't access an instance variable from static methods
// so when the correct answer was no there (line 13) this code was giving an answer "nameless cat"
// in other words this static method below was accessing instance variable in line 10, how come???
public static void main(String[] args) {
Cat cat = new Cat();
cat.setName("Charlie");
System.out.println(cat.name);
}
}
package en.codegym.task.jdk13.task04.task0403;
/*
What's the cat's name?
*/
public class Cat {
//this is an instance variable
private String name = "nameless cat";
public void setName(String name) {
this.name = name;
}
//this is a static method
public static void main(String[] args) {
Cat cat = new Cat();
cat.setName("Charlie");
System.out.println(cat.name);
}
}