0

As mentioned in the question, when I was going through the Sourcecode of android ImageView I saw the variable private Matrix mDrawMatrix = null; which has class level scope and the value of the variable is again assigned to a final variable in a member function(final Matrix matrix = mDrawMatrix;). Here is the function ,

 private boolean isFilledByImage() {
    if (mDrawable == null) {
        return false;
    }

    final Rect bounds = mDrawable.getBounds();
    final Matrix matrix = mDrawMatrix;
    if (matrix == null) {
        return bounds.left <= 0 && bounds.top <= 0 && bounds.right >= getWidth()
                && bounds.bottom >= getHeight();
    } else if (matrix.rectStaysRect()) {
        final RectF boundsSrc = mTempSrc;
        final RectF boundsDst = mTempDst;
        boundsSrc.set(bounds);
        matrix.mapRect(boundsDst, boundsSrc);
        return boundsDst.left <= 0 && boundsDst.top <= 0 && boundsDst.right >= getWidth()
                && boundsDst.bottom >= getHeight();
    } else {
        // If the matrix doesn't map to a rectangle, assume the worst.
        return false;
    }
}

the value of mMatrix is again assigned to a final variable inside the function. Is there any advantage in assigning a variable like that.

99% of us don't know who wrote the code for image view, But if it is a coding practice or if it has any advantages or if someone have an explanation :-) please share it. Thanks in advance from a curious coder. :-)

Sony
  • 7,136
  • 5
  • 45
  • 68
  • 1
    Not me, but maybe due to lack of research? Anyhoo, here are a couple I found in a quick search: http://stackoverflow.com/questions/40236689/is-assigning-a-frequently-used-field-to-a-local-variable-more-efficient http://stackoverflow.com/questions/2785964/in-arrayblockingqueue-why-copy-final-member-field-into-local-final-variable – Mike M. Feb 07 '17 at 04:07
  • @MikeM. i did a google search and this is the link and the results (https://www.google.co.in/search?espv=2&q=assigning+a+class+level+variable+to+a+variable+in+a+function+java&oq=assigning+a+class+level+variable+to+a+variable+in+a+function+java&gs_l=serp.3...9540.9540.0.10753.1.1.0.0.0.0.97.97.1.1.0....0...1c.1.64.serp..0.0.0.xtJPo9_L6Is) that is why i asked here – Sony Feb 07 '17 at 04:21
  • @MikeM. the first link you gave is useful. Please make your comment as answer so that i can accept it as answer. And thanks. – Sony Feb 07 '17 at 04:24
  • 1
    Yeah, I will say that one isn't really easy to search for. I finally wanted the answer, too, not long ago, and it took a little bit to get the search pared down to something useful. I just knew what to use, this time. Anyway, links to other answers isn't really an answer, and I don't wanna just repeat what they have there. If you want to keep this question, I can close it as a duplicate. Or you could just delete it, if you want. You'll get that rep back. – Mike M. Feb 07 '17 at 04:27
  • 1
    @MikeM. i dont care about the reputation i lost, i just want the aswer, that's it. the link you gave me was useful to me, that is why i asked you to put it as an answer. **Anyway, links to other answers isn't really an answer** Thanks for the info too :-) – Sony Feb 07 '17 at 04:30
  • I don't get the question. Where in the function is there an assignment into `mMatrix`? Where i the function is there _any_ reference to `mMatrix`? – ajb Feb 07 '17 at 05:10

0 Answers0