Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
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 1186 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-12-19 22:46:18 »

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

public class TerrainEditor{ 
      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

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

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.


Just realized I have seen things similar to this before:

public void method(Class<Object> object){

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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline saucymeatman
« Reply #1 - Posted 2013-12-20 01: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 02: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

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CodeHead

JGO Knight

Medals: 51

From rags to rags.

« Reply #3 - Posted 2013-12-20 02: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 04:11:14 »

ah, thanks

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Troubleshoots

JGO Knight

Medals: 36
Exp: 7-9 months

Damn maths.

« Reply #5 - Posted 2013-12-20 13: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:

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

Coldstream24 (17 views)
2015-09-03 00:41:28

Andrew_3ds (28 views)
2015-09-01 19:08:10

afikri (19 views)
2015-08-31 09:30:22

afikri (26 views)
2015-08-31 09:30:07

afikri (13 views)
2015-08-31 09:27:24

afikri (17 views)
2015-08-31 09:26:40

Roquen (30 views)
2015-08-29 11:30:54

GamerC4 (37 views)
2015-08-22 20:38:50

GamerC4 (34 views)
2015-08-22 20:37:18

GamerC4 (41 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!