Hello Members,

I am beginning to work with the JGrapht library. The following is described as the syntax for creating a weighted, directed graph:

SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>  graph = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);

What is the meaning of including " DefaultWeightedEdge.class" in the above line?

I will be grateful for any replies.

Thank you!

DefaultWeightedEdge.class is a simple edge factory. It is a default way of making your edges. So by calling that you save yourself the trouble of having to make and define your own edge factory. Take a look in the api http://www.jgrapht.org/javadoc/
I hope this helps

Hello Mattox,

Thank you for the reply and the API reference.

I was just a little confused as to why a .class file would be included in a .java file in such an explicit manner. Any comments?


Despite appearances that ian't a file. The .class operator is used with a named class to get the Class Object representing that class. So
returns an instance of class Class representing the DefaultWeightedEdge class.

It's passed as a parameter to a method to tell the method what kind (class) of Object you want to be created in that method. It's needed as a work-around for a limitation in Java 1,5 types that means you cannot do this:

void myMethod(T param) {
  new <T>();

but you can do this

void myMethod(Class paramClass) {

ps: more info if you're interested

Edited 5 Years Ago by JamesCherrill: fixed type

Hello James,

That was very helpful! Thank you also for the link.


This question has already been answered. Start a new discussion instead.