0

How to access or use in a condition the variable from another class ?? I have a declared variable makol in kstemmer class and i want to use that in stemmer class..

public class Kstemmer {
    private int makol=0;
}

//and this for the stemmer class

public Stemmer() {
  if (makol==0){
    System.out.println("avid");
  }
}
Zain
  • 95
  • 1
  • 8
allan
  • 11
  • 2
  • 3
    Possible duplicate of [Java: How to access methods from another class](https://stackoverflow.com/questions/6576855/java-how-to-access-methods-from-another-class) – 001 May 11 '18 at 15:08

1 Answers1

1

A variable that is private cannot be used from another class. You have to make it public - if they are in the same package you may also leave away both private and public.

Also, that variable is not static. If you want to use it globally, you have to use static int makol = 0; and then reference it with Kstemmer.makol.

Alternatively you can instanciate an Object of Kstemmer with Kstemmer someObject = new Kstemmer() and access the variable with someObject.makol.

Depending on use case, you would use getters and setters instead of making the variable public. Non-final variables should almost always be used with getters and setters.

Zain
  • 95
  • 1
  • 8