Answer the question
In order to leave comments, you need to log in
How in Java to refer to the name of an instance of a class in a method located in the body of the same class?
What I mean:
Let's say there is a Dog class within the same package:
package tpack;
public class Dog {
String name;
public Dog (String name) {
this.name=name;
}
public void bark() {
System.out.println(name + " says: 'Woof-woof!'");
}
}
package tpack;
public class Main {
public static void main (String[] args) {
Dog dog1 = new Dog("Butch");
dog1.bark();
}
}
Butch says: 'Woof-woof!'
dog1 says: 'Woof-woof!'
Answer the question
In order to leave comments, you need to log in
It's impossible to do so.
the name of the specific Dog class object from which this .bark() is called
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question