I'm trying to create two static objects of class Test
: CAT and DOG. I want them to be able to have different output int talk()
method:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hi");
Test.DOG.talk(); //"I am a cat" output
}
}
class Test {
public static Test DOG = new Test("I am a dog");
public static Test CAT = new Test("I am a cat");
public static String talkString;
private Test(String text) {
talkString = text;
}
public static void talk() {
System.out.println(talkString);
}
}
However the CAT object overwrites the talkString
therefore the DOG can no longer use it. Is there a way around it, while keeping DOG and CAT static themselves?