简体   繁体   中英

How to access or use in a condition the variable from another class

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");
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM