Why “pm.newQuery(Employee.class)” and not “pm.newQuery(Employee)” in JDO?
Question
In this JDO, why is .class
needed here?
Query averageSalaryQuery = pm.newQuery(Employee.class);
I would prefer to write this more concise syntax if possible?
Query averageSalaryQuery = pm.newQuery(Employee);
Solution
Query averageSalaryQuery = pm.newQuery(Employee);
Well, it still has to be valid Java, which the above is not...
The way to refer to a class in Java is using the .class
syntax.
OTHER TIPS
Using the ".class" suffix is a convention of the language, so your second example is just semantically invalid. Nothing you can do about it really. It's equivalent to calling the getClass()
method on an instantiation of the class, like:
Query averageSalaryQuery = pm.newQuery(new Employee().getClass())
So it's already a shortcut ;)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow