Java developers always use private access level for methods which are not used outside of this class. There are known benefits of doing so but from other side we increase complexity of unit tests. In most of cases our code is not used by any other services/APIs and we actually don't care of 'private' benefits. But what I believe we do care is to create readable simple unit tests. Considering this, why do not create all methods in class as 'package-private' by default and make them 'private' only in case when we really need this?
Why private access for methods is more preferable than package-private?
369 Views Asked by Aliaksei Yatsau At
1
There are 1 best solutions below
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in OOP
- Access objects variable & method by name
- Why does compiler recognize while(true) at compile time but not if(true)
- Pass variables to extended class
- Cast a superclass type to a subclass type?
- Understanding difference in Swift properties for structs and classes in assignment
- Does exist any way to force child class to have at least one field with a specified attribute?
- Symfony : is it better to use a trait or an intermediary class to complete Controller one?
- (Java) What kind of argument is this? With a
- C++ Implementing a Queue of cars in OOP
- Inheritance in openERP (odoo)
- missing 1 required positional argument: 'key'
- how can Object class in ruby be an instance of it's subclass, class "Class"
- How to force others to obey a specific layout for a child class?
- Class variables in OOP
- define_method in a class method
Related Questions in PRIVATE
- Why can a private class method be explicitly invoked in Ruby?
- Needing a private and public method for the same recursive function
- Check if a private function exists inside an object in JavaScript
- how to get the private meta data from vimeo private video
- When to use @objc in Swift?
- How can I write this private method for being unique to the ticket number, and set that ticket number constructor?
- Calling private function by upcasting
- Find which user viewed my website (on private network)
- C++: what is the proper way of declaring function prototype in anonymous namespace?
- Why I cant access this private variable?
- returning reference to private vs public member
- Typescript - Overloading private method
- C++ Isn't this a useless inline declaration?
- How do I put private in front of many variables at once in android studio?
- Why class object as pointer gives segmentation error, when the usual class object does not
Related Questions in ACCESS-LEVELS
- How can events be handled when the class is static?
- Determining access level of a field
- Why can't extensions with protocol conformances have a specific access level?
- Protected & internal, not protected or internal
- Default security parameter in classes
- Access level of property in Private Class
- XmlSerializer treated as private, although is public
- Protection Levels and Visual Studio unit tests with C#
- How can I add user/access levels to my current login script?
- Swift: Calling function from a different target
- Enforcing Access levels in php
- C# protected field to private, add property--why?
- How to restrict access to nested class member to enclosing class?
- Displaying levels of access for different types of accounts in a UML diagram
- Java define a explicit package-private modifier
Related Questions in PACKAGE-PRIVATE
- Why I cannot access a package-private class in another jar (NOT sealed)?
- Why doesn't C# have package private?
- Java - Difference between private and package-private enum constructor
- Why can a enum have a package-private constructor?
- When would I use package-private in Java?
- Is there a way to pass a package private class to static method
- Make an internal function visible for unit tests
- Enum implementing interface, interface and method visibility
- Internal Package Protected access modifier in Java
- How does a java compiler resolve a non-imported name
- Why private access for methods is more preferable than package-private?
- Is it possible to extend a package private class defined in a 3rd party jar in Scala
- How to create mock object of a class which is package private
- Are packages (that share a name) combined during compliation?
- Package-private class visible to some other packages (with same name) under a different source folder
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
I would not say that it is preferable.
Both practices are acceptable and don't shock me.
Now
privateshould be favored as much as possible as it prevents undesirable coupling whilepackage-privatethat creates a little more coupling should be used with relevancy.Even as you develop a library to be used by some clients, using the package modifier is not bad as the classes in the
package-privatedoesn't make part directly of the API.Reversely, unit testing and
privatemodifiers are not incompatible at all.I agree that using
package-privatemodifier instead ofprivateallows to test/mock/switch easily members of classes.In some cases, this practice is acceptable.
Now generalizing this is before all a design choice.
Personally I use the
package-privatemodifier only as really relevant (a real issue for testing or a internal processing to share among other classes of the same package) because beyond making unit testing easier (which is a good point), I think that this kind of practice may favor a design that couples the classes more strongly than required.For example, suppose in a
Bclass you may access a field/method of aAclass instance because these are in the same package.Suppose that conceptually this field make part of a very internal part of the
Aimplementation and that it have not be known by other classes to allows to change it later.But as this field/method is package-private, now nothing prevents you from making it as you can.
I don't like that.
To finish, I would say that you should not worry about unit testing of
privatemethods.Internals don't make part of the API. You don't need to test it.
Now if the private method is invoked multiple times or you really need to test this processing, nothing prevents you from extracting it in another class to make it testable and to have a way to mock the processing if required (to avoid test it multiple times).