How can I call boolean return value into other method?

class DirectMappedCache extends Cache {

    private int logLineSize;    //log_2 of the number of bytes per cache line
    private int logNumLines;    //log_2 of the number of cache lines
    private TreeMap<String, CacheSlot> tmap; 

    public DirectMappedCache(int logLineSize, int logNumLines) {
    //constructor that takes lengths of different fields
        this.logLineSize = logLineSize;
        this.logNumLines = logNumLines;
        tmap = new TreeMap<String, CacheSlot>(); 

public boolean read(String addr) {

    System.out.println("Read from address " + addr + ": ");
    String tag = addr.substring(0,9); 
    String slotnumber = addr.substring(9,13);
    String offset = addr.substring(13,16); 

        CacheSlot temp = tmap.get(slotnumber); 
                return true; 



        CacheSlot put = new CacheSlot(); 
        put.valid = true; 
        put.tag = tag; 
        tmap.put(slotnumber, put); 

    return false;

 public int numHits(){

        int HitCounter = 0; 
            return HitCounter++; 


        return 0; 

I am making a Cache Simulator but I don't think my numHits() is working because the way I'm calling the value is wrong.

Show source
| java   | caching   | boolean   | return-value   2016-10-25 07:10 1 Answers

Answers to How can I call boolean return value into other method? ( 1 )

  1. 2016-10-25 07:10

    The problem is not the way you call the read() method. Your numHits() method always returns 0 because you return the value of a local variable, which is always initialized to 0.

    numHits() would only make sense if HitCounter becomes an instance variable :

    private int HitCounter = 0;
    public int numHits(){
        if(read(addr)){ // you should replace addr with some variable that you actually declare
            return ++HitCounter; 
        return 0; 

    I also changed return HitCounter++ to return ++HitCounter, since post increment operator (return HitCounter++) will return the old value of HitCounter instead of the incremented value.

    EDIT : Another issue is that you pass to read a variable that isn't declared anywhere. You should decide what you want to pass to that method.

Leave a reply to - How can I call boolean return value into other method?

◀ Go back