Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (800)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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  
  returning a new object from an array  (Read 1830 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2014-04-20 20:21:40 »

so I am wanting to return a new object of the object type that is in an array. Is it possible, for example of waht I want to do   objecta[] as = new objecta[1]; return new objecta[1]
Offline lcass
« Reply #1 - Posted 2014-04-20 21:25:45 »

what if im attempting to do it to an abstract ?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #2 - Posted 2014-04-20 21:30:26 »

I can't tell exactly what you mean, is it something like this? http://stackoverflow.com/questions/529085/how-to-generic-array-creation
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lcass
« Reply #3 - Posted 2014-04-20 21:38:04 »

I have a super Tile() which has subclasses such as space and hull etc the Tiles[] arraystores the subclasses as objects, how would I return a new one.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #4 - Posted 2014-04-20 21:46:10 »

Why not have a
Class<? extends Tile>[]
? Then you can just tiles[3].newInstance();

There's probably a better solution, but to keep your array format, this is what I would do.
Offline Danny02
« Reply #5 - Posted 2014-04-21 08:40:02 »

Pls don't use reflection in this way.
One better solution would be to use factories. Factories are Objects/Classes which know how to instantiate a specific Object.

I don't know why you want to store these factories in an array, but you could use Enums for example:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
interface Factory<E>
{
  E create();
}

enum TileFactories implements Factory<Tile>
{
   GRASS {
      Tile create(){ return new GrassTile(); }
   },
   ROCK {
      Tile create(){ return new RockTile(); }
   },
   SPECIAL_ROCK {
      Tile create(){
        RockTile t  = new RockTile();
        t.setSpezial(true);
        return t;
      }
   }
}


Tile t = TileFactories.getValues()[1].create();
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (348 views)
2019-09-04 15:33:17

hadezbladez (5132 views)
2018-11-16 13:46:03

hadezbladez (2013 views)
2018-11-16 13:41:33

hadezbladez (5400 views)
2018-11-16 13:35:35

hadezbladez (1121 views)
2018-11-16 13:32:03

EgonOlsen (4539 views)
2018-06-10 19:43:48

EgonOlsen (5396 views)
2018-06-10 19:43:44

EgonOlsen (3083 views)
2018-06-10 19:43:20

DesertCoockie (3977 views)
2018-05-13 18:23:11

nelsongames (4527 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04: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!