Bit of Syntax Confusion

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


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


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:


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?

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?

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:

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

ah, thanks

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 .

The only good reason to use Class is for reflection.