java - Creating a static instance variable for return type -


i have game loop runs approximately @ 60 fps. in each iteration need call bunch of methods utility class physics. looks this

public final class physics {      private static final float g = 9.81f;     private static vector2d var1 = new vector2d();      private physics() {     }      public static vector2d method1(param1, param2, param3) {         /*          * computations param1, param2 , param3,           * assign result var1          */         return var1;     } } 

what pros , cons of design pattern? use var1 helper instance variable did in above code because if put inside method1() following

    public static vector2d method1(param1, param2, param3) {         vector2d var1 = new vector2d();         /*          * computations param1, param2 , param3,           * assign result var1          */         return var1;     } 

every second 60 new objects of type vector2d , want avoid garbage collection.

i try minimize variable scope (for readablility) unless there's reason not or significant performance benefit. in case assume vector2d doesn't use overhead , performance benefit of making static variable negligable.

so if don't plan access var1 somewhere else in method1 i'd recommend second design pattern. if possible don't event have declare varable inside method, this:

public static vector2d method1(param1, param2, param3) {     // computations, compute x , y, assign result var1     return new vector2d(x, y); } 

in end it's matter of personal taste.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -