I'm just starting with Java, although I already have some experience with OOP in PHP
In this example, I have a simple class called Person, with fields such as name, age, and sex. I was wondering what is considered the best practice, to do something like this:
public class Main {
public static void main(String[] args){
Person customer = new Person("Jonh Smith", 21, 'M');
System.out.println(customer.name);
}
public static class Person {
public String name;
public int age;
public char sex;
public Person(String name, int age, char sex){
this.name = name;
this.age = age;
this.sex = java.lang.Character.toUpperCase(sex);
}
}
}
In this way, the Person class needs to be static for it to work, and this is what made me wonder that it might not be the best way to do it, here is the other way:
public class Main {
public static void main(String[] args) {
Person customer = new Person("Jonh Smith", 21, 'M');
System.out.println(customer.name);
}
}
class Person {
public String name;
public int age;
public char sex;
public Person(String name, int age, char sex){
this.name = name;
this.age = age;
this.sex = java.lang.Character.toUpperCase(sex);
}
}
So, what's the best way to do this moving forward? I will still need to add other classes, like Company.