Are Java static calls more or less expensive than non-static calls?

前端 未结 12 2377
夕颜
夕颜 2020-11-27 11:57

Is there any performance benefit one way or another? Is it compiler/VM specific? I am using Hotspot.

12条回答
  •  [愿得一人]
    2020-11-27 12:39

    I would like to add to the other great answers here that it also depends on your flow, for example:

    Public class MyDao {
    
       private String sql = "select * from MY_ITEM";
    
       public List getAllItems() {
           springJdbcTemplate.query(sql, new MyRowMapper());
       };
    };
    

    Pay attention that you create a new MyRowMapper object per each call.
    Instead, I suggest to use here a static field.

    Public class MyDao {
    
       private static RowMapper myRowMapper = new MyRowMapper();
       private String sql = "select * from MY_ITEM";
    
       public List getAllItems() {
           springJdbcTemplate.query(sql, myRowMapper);
       };
    };
    

提交回复
热议问题