Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Having trouble with ArrayList  (Read 277 times)
0 Members and 1 Guest are viewing this topic.
Offline xsi3rr4x

Junior Member





« Posted 2014-04-06 22:05:25 »

Hey guys,
I'v got an interface named Entity.
In a separate class I am trying to make an array list of "Entity", here is my code for it.
1  
2  
3  
 Player player1 = new Player();
   public static ArrayList<Entity> entities = new ArrayList <Entity>();
   entities.add( player1);


on line 3 I keep getting an error that on the period that says "misplaced construct" and another error on line 3 where it says player1 that reads "variable decorator id expected after this token"

I'm pretty stuck here and any help would be greatly appreciated (:
Offline BurntPizza
« Reply #1 - Posted 2014-04-06 22:12:04 »

All statements must be in a method body. (A statement is pretty much anything besides a declaration or an allocation, so long as the allocation is part of a declaration.)
I'm guessing these 3 lines (or at least line 3, a statement) are(is) not.

EDIT:

1  
2  
3  
4  
class demo {
   ArrayList<Object> list = new ArrayList<Object>(); //this is fine here
  list.add(null); //<-- Misplaced construct! Needs to be in a an initialization block (static or not) or a method of some type
}
Offline philfrei
« Reply #2 - Posted 2014-04-06 23:07:16 »

All statements must be in a method body.

Statements can also exist in a static initializer block. However, for that to work in the OP's case, player1 would have to be static, too. The following compiles.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
import java.util.ArrayList;

public class StaticTest
{
   static Player player1 = new Player();
   public static ArrayList<Entity> entities = new ArrayList <Entity>();

   static {
      entities.add(player1);
   }
}

class Player extends Entity
{}

class Entity
{}


But most likely, yes, the right answer is probably to wrap the .add() in a method or constructor.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #3 - Posted 2014-04-06 23:19:13 »

Yes, they can also be in a static init block or even an instance init block (fancy), as stated off to the side in the code snippet, but generally and for simplicity they exist in methods.
Offline LiquidNitrogen
« Reply #4 - Posted 2014-04-06 23:19:35 »

Don't forget, you can do the following:

1  
entities.add(new Player());
Offline jonjava
« Reply #5 - Posted 2014-04-07 00:48:43 »

It must be said that you should not be creating static methods or static init blocks to solve a problem - especially one as silly as this. They should be used when intended.

You should never be using static unless you know exactly what you're doing.

To test small snippets of code you can stick it inside the objects main method but preferably you should do it inside the objects constructor or call the objects methods from the main method where you instantiate your object.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
class Game {

  Player player = new Player();
  List<Entity> entities = new ArrayList<Entity>();

  public Game() {
     entities.add(player);
     System.out.println("The Game");
  }

  public static void main(String[] args) {
    new Game();
  }
}

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!