1

A method in BayesianClassifier calls the method below (a method of Category):

public void updateProbabilities(Map<String, int> woordfrequenties) {
    for (Map.Entry<String, int> woordfrequentie : woordfrequenties.entrySet()) {
        String woord = woordfrequentie.getKey();
        int frequentie = woordfrequentie.getValue();
        int index = BayesianClassifier.getVocabulary().indexOf(woord);
    }
}

Now, it states that the non-static method getVocabulary from BayesianClassifier cannot be referenced from the static context here, which I understand, but how then can the method get the value of field 'vocabulary' from the instance of BayesianClassifier that calls this method? It surely must be possible without passing the whole vocabulary as a parameter, or giving the class Category the instance of BayesianClassifier as a field?

  • What if an object of some other class called your method? How would you know how to obtain the desired index? – John Bollinger Dec 08 '15 at 21:29
  • What is wrong with just passing `vocabulary` as a parameter? It's not that long, and causes no issues. Technically, though, you can analyse the stack trace using Reflection (generally, DO NOT DO THIS, however). See an exact duplicate: http://stackoverflow.com/questions/15329566/how-to-find-the-object-that-called-a-method-in-java – bcsb1001 Dec 08 '15 at 21:29
  • Generally speaking, a method -- particularly a public method -- knows nothing about who invoked it. It has only its arguments and the object on which it was invoked to work with. And this is good. – John Bollinger Dec 08 '15 at 21:31
  • Thank you all for your replies! I will just give the class Category a field containing the calling class BayesianClassifier. –  Dec 08 '15 at 21:39

2 Answers2

0

You can use it's using following code:

public class Category {
private final BayesianClassifier bayesianClassifier;

public Category(BayesianClassifier bayesianClassifier) {
  this.bayesianClassifier = bayesianClassifier;
}

public void updateProbabilities(Map<String, int> woordfrequenties) {
    for (Map.Entry<String, int> woordfrequentie : woordfrequenties.entrySet()) {
        String woord = woordfrequentie.getKey();
        int frequentie = woordfrequentie.getValue();
        int index = bayesianClassifier.getVocabulary().indexOf(woord);
    }
} 

or

public class Category {
private BayesianClassifier bayesianClassifier;

public void setBayesianClassifier(BayesianClassifier bayesianClassifier) {
  this.bayesianClassifier = bayesianClassifier;
}

public void updateProbabilities(Map<String, int> woordfrequenties, BayesianClassifier bayesianClassifier) {
    for (Map.Entry<String, int> woordfrequentie : woordfrequenties.entrySet()) {
        String woord = woordfrequentie.getKey();
        int frequentie = woordfrequentie.getValue();
        int index = bayesianClassifier.getVocabulary().indexOf(woord);
    }
} 

or

public void updateProbabilities(Map<String, int> woordfrequenties, BayesianClassifier bayesianClassifier) {
        for (Map.Entry<String, int> woordfrequentie : woordfrequenties.entrySet()) {
            String woord = woordfrequentie.getKey();
            int frequentie = woordfrequentie.getValue();
            int index = bayesianClassifier.getVocabulary().indexOf(woord);
        }
    }
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
  • Yes, I will use this, thank you! I just wanted to check if there was a way of doing this without adding an instance of BayesianClassifier as a field to Category. Thanks again! –  Dec 08 '15 at 21:42
0

As stated in comments it can be done with Reflection. If you cannot pass vocabulary as a parameter then go with the answer here: Previous Answer to Same Question

If you do not want to use Reflection you have two options:

Option 1:

Just pass in the Vocabulary object when calling updateProbabilities. Java is pass-by-value, however this does not mean that the entire Vocabulary object will be copied and passed. This is explained here. Essentially, the value of the pointer to your object will be passed, and in doing so only take up the extra space of one "pointer".

Option 2:

When creating your Category object, add the BayesianClassifier as a field.

Community
  • 1
  • 1
Troy Stopera
  • 302
  • 3
  • 15