PDA

View Full Version : [SOLVED] Simple Java Instance Question



JCoster
September 23rd, 2009, 12:51 PM
Hi guys,
Just a quick question.
I currently have a main class in Java defined as follows:

import java.util.ArrayList();

public class Graph {
private ArrayList<Vertex> exampleList;

public static void main(String[] args) {
// create the graph instance
new Graph();
}

public Graph() {
exampleList = new ArrayList<Vertex>;
// Populate the list as an example
exampleList.add(new Vertex("example1"));
exampleList.add(new Vertex("example2"));
}

public Integer getValue(Vertex tempVertex) {
return exampleList.get(tempVertex);
}

public void printMessage() {
System.out.println("Added successfully.");
}
}

This is just an example... I actually want to do something more meaningful than what I'm about to say BUT, from creating each vertex, can I call the printMessage() function from within the Vertex class?

For example:

public class Vertex {
private String value;

public Vertex(String value) {
this.value = value;

/*
* Now I want to call the 'printMessage()' method from this
* class.
*/
}

}

Of course I know I could put the printMessage() method inside the Vertex class but really I want to do a method which can return a vertex from the ArrayList to the Vertex.

So it's actually more a case of the printMessage() method being:


public Vertex returnVertex(String value) {
Vertex tempVertex = ..get vertex through some code and
the Value parameter..
/*
* Where temp vertex is the vertex I've selected from some code
* above.
*/
return tempVertex;
}

Actually, I guess a simpler way to put this is that from each vertex in the ArrayList, I would like to be able to call a method in the Graph class from the Vertex class which allows me to get a different Vertex into the original Vertex instance.

And yes, I'm very bad at explaining things...

slavik
September 23rd, 2009, 03:27 PM
no, you can't.

printMessage is part of the Graph class and Vertex isn't told about who is initializing it. Besides, the Graph knows if the Vertex was created without issues.

dwhitney67
September 23rd, 2009, 03:57 PM
You could expand the Vertex class to maintain a reference to its Graph container.



public class Vertex {
private String value;
private Graph graph;

public Vertex(String value, Graph graph) {
this.value = value;
this.graph = graph;

this.graph.printMessage();
}
}


The Graph constructor would be something like:


public Graph() {
exampleList = new ArrayList<Vertex>;
// Populate the list as an example
exampleList.add(new Vertex("example1", this));
exampleList.add(new Vertex("example2", this));
}

Now whether this is a good design is debatable.

Reiger
September 23rd, 2009, 04:21 PM
You can have methods like this:


public Vertex deriveVertex(Vertext original) {
/* stuff here */
}


As well as static equivalents (just add the static keyword after public); called like this: Vertex.deriveVertex(Vertex original);

This technique can be used if your Graph instance is static:


public class Graph {
public static Graph graphInstance = new Graph();
// other stuff here
public static Vertex deriveVertex(Graph theGraph) {
// stuff here...
}
}

class Vertex {
public void test() {
Graph.deriveVertex(Graph.graphInstance);
}
}

JCoster
September 23rd, 2009, 05:40 PM
Thanks for all your replies, but I've decided to go for this implementation:


You could expand the Vertex class to maintain a reference to its Graph container.



public class Vertex {
private String value;
private Graph graph;

public Vertex(String value, Graph graph) {
this.value = value;
this.graph = graph;

this.graph.printMessage();
}
}


The Graph constructor would be something like:


public Graph() {
exampleList = new ArrayList<Vertex>;
// Populate the list as an example
exampleList.add(new Vertex("example1", this));
exampleList.add(new Vertex("example2", this));
}

Now whether this is a good design is debatable.

Thank you once again.