dpm dpm - 3 months ago 13
Java Question

Java: Cannot access methods from a different class

First Class: Graph.java

public class Graph {

public void generateGraph(int VertexNum, int numOfEdges) throws ZeroVerticesException, DisjointGraphException{
//Statements
}
//More methods
}


Second class : prims.java

public class prims {

Graph g=new Graph();

g.generateGraph(10,20); // Error here

}


The error shows as: "Syntax error, insert ")" to complete MethodDeclaration"

Why can't i access the method after creating an instance of graph ?

Answer

In Java, your code statements should be part of some method or a block

In your case, at least main method, if that is your main class.

The code can't just be inside class body, outside any block

Following code should probably help what you are trying to achieve

public class prims {

    public static void main(String args[]) {
        Graph g=new Graph();

        g.generateGraph(10,20); // Error here
    }

}