简体   繁体   中英

what's user of static object filed in service layer

in my class AccountService ther is Static filed AccountDao which is also class why we need to declare as static i know this will we use for accessing
classname.staticfield access every where in class but what use for with other class object declare as static?? thanks in advance

 public class AccountService {

private static AccountDao accountinfo;

public AccountService() {
    accountinfo = new AccountDao();
} }

Static members are shared by all instances of a class. In your example, all instances of AccountService may access just one instance of AccountDao called accountinfo .

Why this is done in your case, I do not know. To make it worse, every time a new instance of AccountService is created accountinfo is instantiated again. That means that an older instance cannot refer to the original object of accountinfo once another instance of AccountService is created.

Generally, there are a number of reasons why static members are used, eg logical (1:n relationship), performance (all objects share one ressource). Whatever the reason is: be careful when and how to (re-)instantiate a static member as all objects of that class may want to use this member at different times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM