Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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  
  Bit of Syntax Confusion  (Read 577 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-12-19 23:46:18 »

I was poking through the source code for a neat little application and saw this bit of syntax:

1  
2  
3  
4  
5  
6  
7  
public class TerrainEditor{ 
      //constructor
     public void makeBlock(Vector3Int pos, Class <? extends Block> block) {
           //creates the block
     }
      //other code
}


No, it wasn't Minecraft by the way.

The bit that confused me was the Class <? extends Block> block. From what I have seen, to use this method you would have to provide something like

1  
editor.makeBlock(/*pos*/, BlockWood.class);


Correct?
If so, what is the point of this? I suppose so that you don't have to instantiate a new object of the class. What does one do with this object though, is it purely for calling static values or something?
It seems like an interesting little piece of Java, I'd like to use it later because if it is what I am thinking it is, it's very useful.
Thanks.

Edit:

Just realized I have seen things similar to this before:

1  
2  
3  
public void method(Class<Object> object){
//stuff
}


I suppose these are related, however I do not know the difference between the one with the '?' and the one w/o. Mind explaining?

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline saucymeatman
« Reply #1 - Posted 2013-12-20 02:37:16 »

Ive never seen a <? extends Foo> thats pretty interesting. Probably just me being inexperienced.
I'd imaging that the <? extends Whatever> would mean that the parameter must be an extension of Whatever, but why not just use an interface?
Offline Slyth2727
« Reply #2 - Posted 2013-12-20 03:22:37 »

Oh yes, did I not mention that was what it meant?
yeah, and if it is just <?> it means it has to extend Object.
I just didn't know what it is used for. Still don't Wink

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CodeHead

JGO Coder


Medals: 35


From rags to riches...to rags.


« Reply #3 - Posted 2013-12-20 03:57:58 »

Generics Wildcard type. Was looking into this the other night for a project.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline Slyth2727
« Reply #4 - Posted 2013-12-20 05:11:14 »

ah, thanks

Check out Escaping Vox, a 3D
|\ _\  voxel based puzzler!
 \|_|
Offline Troubleshoots

JGO Coder


Medals: 35
Exp: 7-9 months


Damn maths.


« Reply #5 - Posted 2013-12-20 14:49:52 »

Look into lower and upper bounded wildcards and PECS and GET and PUT rule. Basically, if you want to get data from an object such as a collection (like a list) you'd use <? extends myObject> and if you want to set data you'd use <? super myObject>. If you want to do both you would use neither.

The example you've given is taking a class object which extends Block. In my opinion that is bad OO design and should be avoided whenever necessary. The only reason you would do something like that is if you couldn't avoid it, for example:

If you wanted to create a vector class which extends Number (please don't do it! - it'd look horrible and require a lot of instanceof checks) it would look something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Vector<T extends Number> {
   
   public T x, y;

   public Vector(T x, T y) {
      this.x = x;
      this.y = y;
   }

   public <T1 extends Number> Vector<? extends Number> changeType(Class<T1> type) { // you have to return a wildcard as generic type arguments are lost at runtime
     return new Vector<type.getObject()>(this.x, this.y); // you couldn't put T1 as the generic type for the same reason as above
  }
   
}


It probably doesn't even work (I wrote it off the top of my head and I don't even know if the getObject method exists) but hopefully you get the idea. You need to get the data of the objects type from the method arguments so you use <T1 extends Number>.

The only good reason to use Class<ObjectType> is for reflection.

Why are all OpenGL tutorials written in Brainf**k?
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.

xsi3rr4x (22 views)
2014-04-15 18:08:23

BurntPizza (17 views)
2014-04-15 03:46:01

UprightPath (31 views)
2014-04-14 17:39:50

UprightPath (15 views)
2014-04-14 17:35:47

Porlus (31 views)
2014-04-14 15:48:38

tom_mai78101 (57 views)
2014-04-10 04:04:31

BurntPizza (115 views)
2014-04-08 23:06:04

tom_mai78101 (214 views)
2014-04-05 13:34:39

trollwarrior1 (182 views)
2014-04-04 12:06:45

CJLetsGame (189 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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!