When is using public fields acceptable Java -


I have a main square with thread pool, which is used by some other classes to perform actions on the database. goes . I currently get a great method to get the pool which works fine, but something looks weird

Is there a situation where it is acceptable to use any public sector instead of gater / setter methods?

Is there a situation where it is used to make use of public sector rather than gater / setter methods Is Acceptable for?

The main reason is that the public sector is bad, they display the implementation in the outside world, there is an unwanted coupling; That is, classes which are more dependent on the details of implementation of other classes. To understand this, the code makes it difficult and it is difficult to change. And if the area is not the last , then you have to find the entire code-base to make sure that there is nothing "interference" with that field. (OK, IDE makes it easy ... but compare a public area with a private area with no setter.)

A secondary reason is that you override an area Once you have exposed the field in a superclass, you can not do anything in the sub-class that can modify or restrict its meaning. (In contrast, gates and the setters can be overridden ...)

The only condition where it is acceptable (from a stylish perspective) is the "public" field, when the class declaring the field The result of a private nested or internal classroom is that all dependencies on the field are restricted to source files, in which the announcement is ... which denies the above problems.

UPDATE - I public forgotten stationary final ... but we do not consider them all to be fields. Anyway, it is normal that the public static final fields are directly used to think of a stable deliberately to expose the name, type and value ... and static Override problem does not apply due to the nature of the areas.


I'm currently getting a method to get a pool that works fine, but looks a little strange.

"Clumsy" is a matter of taste / taste. Personally, I do not think that comparing with obj.getInstance () obj.instance is clumsy. This is just the Java method 1 . Flipside is that if you do not have the getInstance () method, all classes should have hard-coded references to the example area in the used pool if (for some reason) You had to make some changes to the way the pool was delivered (for example, add a security check, add a counter, make the lazy to make the pool, make sure the access is properly synchronized)Programmed to change every place where you have coded in terms of area. But with the recipient, you have just one place.

1 - Obviously, in other languages ​​this happens in a different way. But you are not writing those languages, Java is what it is.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -