This is because you are calling a non-static class in the static context. One way is to create the Person object in your main class and then you create the main class object. Otherwise, you should declare the Person class as static class such as
import javax.swing.JOptionPane;
public class Main{
public static void main(String[] args){
Integer age1 = Integer.parseInt(args[1]);
Integer age2 = Integer.parseInt(args[3]);
// Create two Person Objects
Person person1 = new Person(args[0], age1);
Person person2 = new Person(args[2], age2);
// toString() method to display first & second Person Object
String firstOutput = person1.toString();
String secondOutput = person2.toString();
JOptionPane.showMessageDialog(null, firstOutput);
JOptionPane.showMessageDialog(null, secondOutput);
}
// Object
static class Person{
// Data fields that will store each of the objects data
private String name;
private Integer age;
// Constructor
public Person(String n1, int a1){
name = n1;
age = a1;
}
public String toString(){
String output = name + " is" + age + " years old.";
return output;
}
}
}
See another example.