1

It is basic, but I am really new in Java... In Controller.class I get the values(centers, dataset, k) with which I start my "boss method"

 Main.kmeans(centers, dataset, k);

this method calls many linked methods in Main.class. One of the methods I need is pobarvajClusterje:

public static Map<String, TockaXY> pobarvajClusterje(List<TockaXY> kmeans, int k) {

    List<String> barve = new ArrayList<>();
    barve.add("Modra");
    barve.add("Rumena");
    barve.add("Zelena");
    barve.add("Rdeca");
    barve.add("Vijola");
    barve.add("Crna");
    barve.add("Oranzna");
    barve.add("Roza");
    barve.add("Rjava");
    barve.add("Siva");

    Map<String, TockaXY> barvniCluster = new IdentityHashMap<>(k);

    for (int d = 0; d < k; d++) {
        barvniCluster.put(barve.get(d), kmeans.get(d));
    }
    return barvniCluster;
}

This method does return a

Map<String, TockaXY> barvniCluster = new IdentityHashMap<>(k);

So how to get this Map in a third WebView.class to iterate over it? Because I have to call the function from WebView.class in Main.class with parameters from Control.class. Or am I declaring functions in wrong way?

Kr Neki
  • 93
  • 5
  • Possible duplicate of [Calling static method from another java class](https://stackoverflow.com/questions/18834005/calling-static-method-from-another-java-class) – Yash Sep 08 '19 at 08:50
  • Thanks for link, it clears a little bit, but I still don't get how to actually implement... I have to call the function from WebView.class in Main.class with parameters from Control.class – Kr Neki Sep 08 '19 at 09:13

1 Answers1

1

In order to call the public static method from Main.class, you'd write something like following in the Control.java.

public static void main(String[] args) {
    Map<String, TockaXY> output = Main.pobarvajClusterje(argument1, argument2);
}

Depending upon the arrangement of your packages, you might have to import Main in your current class in order to call this method as I've mentioned.

The main method is just for example, you can of course call from inside any method in Control class.

Yash
  • 11,486
  • 4
  • 19
  • 35
  • Like this Eclipse is happy: Map output = Main.pobarvajClusterje(Main.kmeans(centers, dataset, k), k); Like this i got the values in method, but now i have to get it in WebView class to iterate the result. Because I can't get it with SampleController.output; – Kr Neki Sep 08 '19 at 09:41
  • 1
    That is so because `output` is not a static variable in your `SampleController`. You can do something like `public static Map output` and use this `output` variable to assign and reuse the values from the method output. But, for a clearer understanding of Java language constructs, I highly recommend reading a good book before jumping into complex TODOs. – Yash Sep 08 '19 at 09:48