Question

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;
      }
   }
}
Was it helpful?

Solution

Person is non static inner class which means it is member of instance of outer class (BasicClass) and without this instance you cant use it (just like methods or fields).

Since you are using this class in static method and in static method there is no default this reference consider

  • making Person class static
  • or creating instance of your outer class via which you will create instance of Person class, like

    BasicClass basic = new BasicClass();
    Person p = basic.new Person();
    
  • or placing code of your Person class outside BasicClass class.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top