Monday, 15 September 2014

performance - In Java, is it worthy to store a field from another class in a field in my class? -


Assume that I have a Java class (which we will call that class ) field (None of them are fields) and assume that I use a lot of time in all those areas, in many ways from each class, which we will call on ThisClass Keeping in mind where both memory usage and performance are important, last client ThatClass , which is the this class in the field as ThisClass ?

Example:

  thatclass public class {public static final fu field 1 = new fu (); Public stable final fu field 2 = new fu (); Public stable final fu field 3 = new foo (); } // implementing a public class ThisClass {public (ThisClass (for (int i = 0; i   

Now this code

  // implement this other implementation to the public class thisClass {Private Final Foo myField1; Private Final Foo MyFilled 2; Personal Files La Foo MyField 3; Public This Class () {myField1 = ThatClass.field1; MyField2 = ThatClass.field2; for MyField3 = ThatClass.field3; (IntiI = 0; I   

I know that in the implementation B, for the method bar pass three areas as arguments It would not be necessary, but imagine this for a discussion that there was a need.

Question:

  1. Is there any difference in performance between the two implementations? Is faster than B?

  2. About the cost of memory, is there a demand for more memory than B? I think it does, but a bit more (an additional reference to each additional area in B. Each reference is an int size, is not it?)

    Thanks!

    1 In addition to what Russell said, there is a violation of the two areas where it has defined area. You give a very trivial example, but imagine that there is a need to change the definition of any of those static areas. With the second implementation you will need to update the code twice - remember that you have two definitions of the same area.

    No comments:

    Post a Comment