I'm making a Card class on Java, and I want it to be as idiomatic as possible. Should I encapsulate all the fields making them private and providing getters as following:
public class Card implements Comparable<Card> {
private char suit;
private String name;
private int value;
public Card(char suit, String name, int value) {
this.suit = suit;
this.name = name;
this.value = value;
}
public char getSuit() {
return suit;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
public String toString() {
return name + " of " + suit;
}
@Override
public int compareTo(Card card) {
return Integer.compare(value, card.value);
}
}
Or since any of the fields are not going to be modified, should make public and final all the fields:
public class Card implements Comparable<Card> {
public final char suit;
public final String name;
public final int value;
public Card(char suit, String name, int value) {
this.suit = suit;
this.name = name;
this.value = value;
}
public String toString() {
return name + " of " + suit;
}
@Override
public int compareTo(Card card) {
return Integer.compare(value, card.value);
}
}
I'm reading Clean Code on the chapter of Data Structures vs OO classes, and I do not know what approach should I take in this case. Thanks in advance!
EDIT: This class is part of a BlackJack I'm developing, and I need to access the fields from another classes.
EDIT: This question has been put on hold, but, where does this question should be posted then? Should I move it to Code Review? I'm truly interested in knowing the opinions of more experienced programmers on this subject, but I want to post it on the right site