Java Class Type

I have a block of code that works and I wanted to ask what exactly is happening here?

Class<?> normalFormClass = null;


The wildcard "<?>" is the part that is confusing for me. Why would this be used rather than not using it (generics)?

Asked by: Emma896 | Posted: 21-01-2022

Answer 1

That means it can be a Class of any type. ? is a wildcard denoting the set of all types, or 'any'. So you can later do

Integer normalForm = new Integer();
normalFormClass = normalForm.getClass();


String normalForm = new String();
normalFormClass = normalForm.getClass();

If you are not aware of generics on Java, read

As for the why, I think it might be to strictly express that you are using generics everywhere and your code is not compatible with older Java versions, or maybe to shut up some trigger happy IDEs. And yes,

Class foo 


Class<?> foo 

are equivalent.

Answered by: Kristian618 | Posted: 22-02-2022

Answer 2

Also, the generic version

Class<?> normalClass = null;

is pretty much equivalent to the raw type version,

Class normalClass = null;

the main difference is that the latter will be compatible with versions of Java that do not support generics, like Java 1.4.

Answered by: Roman285 | Posted: 22-02-2022

Answer 3

Class<T> means that since JDK 1.5, Class is generified by the type the class defines. One way to think about this is that Class is a factory for creating instances of its type.
Specifically, a Class<T> creates instances of type T from the Class.newInstance() method.

From JDK 1.5 on, it is recommended by the Java Language Spec that you should not use raw types. So, it is highly recommended that when dealing with a Class instance of unknown type, you should refer to it as "Class<?>" and not just "Class". Of course, if you actually know the type or some bound, you might find some benefits of specifying it.

Answered by: Tara351 | Posted: 22-02-2022

Answer 4

Class or Class<?> works equally well. One reason to use the latter is to get rid of a few warnings the ide or compiler would throw at you when using the first form.

Answered by: Marcus220 | Posted: 22-02-2022

Answer 5

I doubt this is homework, since I've never been in a class that talked about reflection.

But this is a simple null assignment. It doesn't do anything.

Answered by: Dainton644 | Posted: 22-02-2022

Similar questions

java - Get a class name with generics information

In a java serialization problem, I want to save some classes name and I have some problems with generic classes. For example : - If I have ArrayList&lt;String&gt; listToDump = new ArrayList&lt;String&gt;(); - If I take the name : listToDump.getName(); or listToDump.getCanonicalName(); - I will have java.util.ArrayList or ArrayList - And I want...

Getting type from class name with Java generics

I have following class, I need to get type in constructor, how can I do that? public abstract class MyClass&lt;T&gt; { public MyClass() { // I need T type here ... } } EDIT: Here is concrete example what I want to achieve: public abstract class Dao&lt;T&gt; { public void save(GoogleAppEngineEntity entity) { // save entity to datas...

Java generics - get class?

This question already has answers here:

java - How to get class type information from generics?

I have a problem, I need to resolve type of class which replaced the generic type parameter. Here is the code: public class EnumSetPlay { private enum SomeEnum {ONE, TWO} private static class Test&lt;E extends Enum&gt; { private Class&lt;E&gt; clazz = (Class&lt;E&gt;) GenericTypeResolver.resolveTypeArgument(getClass(), Test.class);; public void test() { for (E element ...

How to get class with generics types in Java

I am trying to make a method call like this, public class GenericsTest&lt;T&gt; { public static &lt;T&gt; Map&lt;String, T&gt; createMap(Class&lt;? extends Map&lt;String, T&gt;&gt; clazz) { return null; } public static void main(String[] argv) { Map&lt;String, Integer&gt; result = createMap(TreeMap.class); } } But I am getting this error, &...

Java Generics Test class

Im new to generics and i have to implement a binary search tree using generics. I did that but now im wondering how do i test the code that i wrote? Do i just make another class and start using the methods of the bst? any help would be appreciated. below is my code just to clarify. public class BST&lt;E extends Comparable&lt;E&gt;&gt; { public Node&lt;E&gt; root; public BST() { root = null; } ...

java - Generics with same base class

I have a TreeNode class defined like so: class TreeNode&lt;T extends MyClass&gt; { public T element; public TreeNode&lt;? extends MyClass&gt; parent; public ArrayList&lt;TreeNode&lt;? extends MyClass&gt;&gt; children; } I would like to know if there is an easier way to restrict all TreeNodes to generic type variables that extend MyClass? Having to write out A...

generics - Lazy class cast in Java?

Can someone please enlighten me as to why I don't get a ClassCastException in this snippet? I'm strictly interested into why it isn't working as I was expecting. I don't care at this point whether this is bad design or not. public class Test { static class Parent { @Override public String toString() { return "parent"; } } static class ChildA extends Parent { @Override pub...

java - How get class for a Generics class?

This question already has answers here:

Can not cast class to generics in java

Please help resolve an issue regarding generics. I tried many ways but it's still not working. Problem is: public static void main(String[] args) { Utils.execute(new TestAction(), new TestCallBack()); } Compiler show error: The method execute(Action&lt;?&gt;, CallBack&lt;?,Action&lt;?&gt;&gt;) in the type Utils is not applicable for the arguments (ImplementClass...

java - Generics in legacy code

We've got a fairly large amount of code that just made the jump to Java 5. We've been using generics in those components targeted at being released in the Java 5 version, but the remaining code is, of course, full of raw types. I've set the compiler to generate an error for raw types and started manually clearing them, but at the present rate it'll take a very long time to go through with it (there are about 2...

Java Generics Syntax for arrays

What data structure does the following declaration specify? List&lt;ArrayList&gt;[] myArray; I think it should declare an array where each element is a List (e.g., a LinkedList or an ArrayList) and require that each List contain ArrayList objects. My reasoning: List&lt;String&gt; someList; ...

generics - How do I write a Java function that returns a typed instance of 'this' and works when extended?

This is a bit of a lazyweb question but you get the rep so :-) I have a Java class that returns instances of itself to allow chaining (e.g. ClassObject.doStuff().doStuff()) For instance: public class Chainer { public Chainer doStuff() { /* Do stuff ... */ return this; } } I would like to extend this class. Is there a way, perhaps using generics, t...

How to use generics in a world of mixed Java versions?

I like generics a lot and use them whereever I can. Every now and then I need to use one of my classes in another project which has to run on an old JVM (before 5.0), needs to run on JavaME (where generics are not allowed neither) or in Microsoft J# (which has VERY poor Support for generics). At the moment, I remove all generics manually, which means inserting many casts as well. Since generics are said to ...

C# vs Java generics

This question already has answers here:

In Java, when should I use "Object o" instead of generics?

For example, I could write either of these: class example &lt;T&gt; { ... public void insert (T data) { ... } } or class example { ... public void insert (Object o) { ... } } Is there a signficant difference between the 2 in terms of performance? With generics I could restrict the type of the parameter and ...

generics - Why can't the Java compiler figure this out?

Why is the compiler unable to infer the correct type for the result from Collections.emptySet() in the following example? import java.util.*; import*; public class Test { public interface Option&lt;A&gt; { public &lt;B&gt; B option(B b, F&lt;A,B&gt; f); } public interface F&lt;A,B&gt; { public B f(A a); } public Collection&lt;String&gt; getColl() { Opt...

java - How do you access Class object for generics?

How can I access Class object for Generics? Currently, I am doing it this way: List&lt;String&gt; list= new ArrayList&lt;String&gt;(); list.getClass(); Is this OK? Or, what should be the way?

How do I fix this Java generics wildcard error?

In this question, TofuBeer was having problems creating a genericized IterableEnumeration. The answer came from jcrossley3 pointing to this link which pretty much solved the p...

Java: Generics won't work for my method, what else can I do?

In the code below, I would like the second method to be generic, too, but since I create the Calendar object inside the method, because of type erasure, I don't see how. One possibility would be to pass in the Calendar object, but that would defeat the main purpose for having this method at all (not h...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)