I'm trying to display my person objects (name, age) using JOptionPane. (Name and age being a string and integer from the commandline argument). However, I'm getting the following error:
BasicClass.java:9: error: non-static variable this cannot be referenced from a static context
Person person1 = new Person(args[0], age1);
^
BasicClass.java:10: error: non-static variable this cannot be referenced from a static context
Person person2 = new Person(args[2], age2);
^
Am I in the right track in initializing the first Person object to commandline input?
EDIT Sorry I forgot my code sample:
import javax.swing.JOptionPane;
public class BasicClass{
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
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;
}
}
}