I want to ask about the effect of static: I made a class music and used static when declaring my music class variables. But when I run my main class to print out music data, it always prints out the same value from the last data I put in in my main class.
my class music :
private static String name, genre;
private static int price;
public music(String a, String b, int c){
name = a;
genre = b;
price = c;
}
my main class code:
music a1 = new music("A","A",1);
music a2 = new music("B","B",2);
music a3 = new music("C","C",3);
music a4 = new music("D","D",4);
System.out.println(a1);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
my output :
Music[Nama = D, Genre = D, harga = 4]
Music[Nama = D, Genre = D, harga = 4]
Music[Nama = D, Genre = D, harga = 4]
Music[Nama = D, Genre = D, harga = 4]
I know how to fix it - I just need to remove static from variable declaration - but I want to know why static makes so much difference and makes my code wrong.
staticwhen used for a class variable?