I'm running into a problem when trying to create an ArrayList in Java, but more specifically when trying to add() to it. I get syntax errors on the people.add(joe); line...

Error: misplaced construct: VariableDeclaratorId expected after this token.
    at people.add(joe);
                  ^

It's my understanding that an ArrayList would be better than an array for my purposes, so my question is, is that the case and if not, where am I going wrong with my syntax?

This is my code...

import java.util.ArrayList;

public class Person {
    static String name;
    static double age;
    static double height;
    static double weight;

    Person(String name, double age, double height, double weight){
        Person.name = name;
        Person.age = age;
        Person.height = height;
        Person.weight = weight;
    }

    Person joe = new Person("Joe", 30, 70, 180);
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(joe);
}
share|improve this question
2  
You need to add things within a block (method, constructor, initializer, etc). – Beau Grantham Jun 20 '12 at 0:33
2  
btw, you're misusing static = all of your People will share the same name, age, etc etc. you want to remove the static keyword and replace "Person.name = " with "this.name=" in the constructor. – Steve B. Jun 20 '12 at 0:36
Thank you for all the help. It is working now within a method. – Jarod Jun 20 '12 at 0:50
feedback

7 Answers

up vote 2 down vote accepted
static String name;      
static double age;
static double height;
static double weight;

Why are these variables defined as static?

It looks like you are doing it in the Person class. Doing it in the class is OK(it can be done), but doesn't make much sense if you are creating an ArrayList of Person objects.

The main point here is that this must be done within an actual method or constructor or something (an actual code block). Again, I am not entirely sure how useful an ArrayList of type Person would be inside of a Person class.

import java.util.ArrayList;

public class Person 
{                   // Don't use static here unless you want all of your Person 
                    // objects to have the same data
   String name;
   double age;
   double height;
   double weight;

   public Person(String name, double age, double height, double weight)
   {
      this.name = name;       // Must refer to instance variables that have
      this.age = age;         // the same name as constructor parameters
      this.height = height;    // with the "this" keyword. Can't use 
      this.weight = weight;    // Classname.variable with non-static variables
   }

}

public AnotherClass 
{
   public void someMethod()
   {
      Person joe = new Person("Joe", 30, 70, 180);
      ArrayList<Person> people = new ArrayList<Person>();
      people.add(joe);
      Person steve = new Person("Steve", 28, 70, 170);
      people.add(steve);            // Now Steve and Joe are two separate objects 
                                    // that have their own instance variables
                                    // (non-static)
   }
}
share|improve this answer
In which class it is done is not of concern. It's the fact that it's being done outside of a method or other block of code. – Beau Grantham Jun 20 '12 at 0:38
@BeauGrantham: Good point and I understand that, I just don't think it makes much sense to do such a thing. – Doug Ramsey Jun 20 '12 at 0:41
feedback

Put curly braces around people.add(joe); and the code will compile:

{people.add(joe);}

Why? Leave as an exercise on initialization blocks.

share|improve this answer
feedback

First, your ArrayList variable really should be at an instance level, or static. Declare it as such:

private ArrayList<Person> people;

or:

static ArrayList<Person> people;

Second, you require some sort of function to perform operations in.

public static void addPerson(Person p) {
    people.add(p);
}

Third, you need to invoke it. You can do this by:

Person.addPerson(new Person("Joe", 30, 70, 180));

in the body of main(), or somewhere that's relevant to your program's execution.

share|improve this answer
feedback

Place your code (after the constructor) in a method then call the method somewhere else. Your fields must not be static for this purpose since all instances will share it.

share|improve this answer
feedback

ArrayList add operation should be done in method block (maybe in main) as others suggested.

public class Person  {
  static String name;
  static double age;
  static double height;
  static double weight;

  Person(String name, double age, double height, double weight) {
    Person.name = name;
    Person.age = age;
    Person.height = height;
    Person.weight = weight;
  }
}

public static void main(String[] args) {
  Person joe = new Person("Joe", 30, 70, 180);
  ArrayList<Person> people = new ArrayList<Person>();
  people.add(joe);
}
share|improve this answer
feedback

write these inside some method or block.
like::

import java.util.ArrayList;

public class Person 
{
   static String name;
   static double age;
   static double height;
   static double weight;

  Person(String name, double age, double height, double weight)
  {
    Person.name = name;
    Person.age = age;
    Person.height = height;
    Person.weight = weight;
  }

  public static void main(String args[])
  {
    Person joe = new Person("Joe", 30, 70, 180);
    ArrayList<Person> people = new ArrayList<Person>();
    people.add(joe);
  }
}
share|improve this answer
feedback

Put that code in a main method like:

 public class Person {

     public static void main(String[] args ) {
         Person joe = new Person("Joe", 30, 70, 180);
         ArrayList<Person> people = new ArrayList<Person>();
         people.add(joe);
     }
 }
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.