public class Cat{
private String name;
public void setName(String name){
this.name=name;//set name by passing current reference of object .
}
public String getName()
return name;//return name or return this.name
//which will correct ?
}
Both will work but I don't understand
In return name; how reference passed .
Can any one will give brief information?How reference will passed?
Resolved
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
9 May 2020, 17:16
That's what OOP is all about.
is not a varible but a field or attribute of the class Cat. Means, if you create an object, each has its own name field. The scope of the field of course is the entire class, or better, you can access this field in all methods. If you access a field in a method, you can prfix it with this.fieldName. That way you see at once, that it's a field and it won't give problems, if you have parameters with the same name e.g. in the constructor. +2