What does dot class mean in the parameter passed to Constructor
I am using jgrapht for the first time . I have this question
What are we passing to the constructor of the DefaultDirectedGraph class? I mean what does DefaultEdge.class means ? There is no static field with that name inside that class. I mean what is actual being passed to the contructor of that class. What does the dot class mean there ?
DirectedGraph<String, DefaultEdge> g =
new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
If the question only aims at the syntax, you might want to refer to the section The .class Syntax of the site about Retrieving Class Objects from the oracle documentation. In general
DefaultEdge.classis an object that represents the classDefaultEdge. This is an object of the typejava.lang.Class, and contains information about the class itself - e.g., which fields and methods this class contains.In this case, this
Classobject is used internally, by jgrapht, in order to create instances of edges. When theGraph#addEdge(V,V)method is called, thisClassobject will be used internally to create an edge - particularly, to create aDefaultEdgeinstance.There are various possible ways how this can be done exactly, but it will either boil down to calling
Class#newInstance(), or to obtaining aConstructorfrom the givenClassand invokingConstructor#newInstance(...), passing the given vertices as parameters.Extended in response to the comment:
For the particular case of the
DefaultDirectedGraph, the creation of the edges is done with anEdgeFactory- a simple Factory that creates edge instances from two vertices. This factory is used in theaddEdgemethod:This
EdgeFactoryis created in the constructor, from the givenClassobject (which may beDefaultEdge.class, as in the example) :The
ClassBasedEdgeFactory, in turn, does what I already mentioned: It uses the givenClassobject to create a new instance:So to summarize: One can pass a
Classto the graph constructor (for example,DefaultEdge.class), to simply tell him: "Whenever I want to add a new edge, then create a new instance of this edge class."