Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 331 times)
0 Members and 1 Guest are viewing this topic.
Offline xsi3rr4x

Junior Devvie





« 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 (:
Online BurntPizza

« JGO Bitwise Duke »


Medals: 289
Exp: 5 years



« 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.

"It's after the end of the world! Don't you know that yet?"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online BurntPizza

« JGO Bitwise Duke »


Medals: 289
Exp: 5 years



« 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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!