I made 2 objects of the cereal class I made. One called frostedFlakes with the name "Frosted Flakes" and one called luckyCharms with the name "Lucky Charms". When I called on an accessor method I wrote to print the name of FrostedFlakes, it printed the name "Lucky Charms".
public class MyProgram
{
public static void main(String[] args)
{
Cereal frostedFlakes = new Cereal("frosted Flakes");
Cereal LuckyCharms = new Cereal("Lucky Charms");
System.out.println(frostedFlakes.getName());
}
}
import java.io.*;
public class Cereal {
//static variables
private static String name;
//constructor
Cereal(String name){
this.name = name;
}
//accessor methods
public static String getName(){
return name;
}
}
I thought maybe there was a problem with the way I wrote the constructor or wrote the variables, so I experimented with both, yet received the same result.