0

I've read the docs for the Junit ExpectedException class, and the parameter for the .expect() method seems to be implemented strangely in all the examples I've studies. When you pass in the expected exception type, it has a .class extension that doesn't appear to be a method of any kind.

Can someone please explain this syntax? Thank you. Example code is below:

public ExpectedException thrown = ExpectedException.none();

public void testPlusException() {
    thrown.expect(RuntimeException.class);
    Vector testVector = vector1.plus(vector3);

}
zephos2014
  • 331
  • 1
  • 2
  • 13

2 Answers2

2

Calling .class on a type gets the class object of the particular type. You can read more about the specifics here.

http://docs.oracle.com/javase/tutorial/reflect/class/classNew.html

Falmarri
  • 47,727
  • 41
  • 151
  • 191
2

Although it's not mentioned in the Java documentation for the Object class, all classes derived from Object have a public static Class<T> class member that stores the runtime type of said object.

It's also what gets returned when you call getClass() on an object, keeping in mind that all methods in Java are virtual.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • Technically, it's not a member. You can't use `class` in every syntax where you can use other static members, and the `getFields()` method of `Class` won't return `class` as one of the members. – ajb Oct 08 '14 at 21:37
  • Also it's not really part of `Object` because you can do `boolean.class` – Falmarri Oct 08 '14 at 22:02