What is the use of package level protection in java?

前端 未结 15 1098
时光说笑
时光说笑 2020-12-05 14:28

I know how package level protection in java works. I read a lot of code (including lots of open source stuff) and no-one seem to be using it. The whole protection l

15条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 15:00

    I am debating many of these issues in my mind currently and this is a good discussion. I have been opting to make all private things private the way they should be, but then I am writing a ton of reflection to unit test privately scoped code. This is, I think the pure OO way to do it. However, I have discovered that it is possible to seal a jar file so that package scope is restricted to inside the module (jar). This, in my mind makes package scope much more palatable. I may be willing to break good encapsulation in exchange for reducing my unit test code to a fraction of what it would be. The thought being that if someone were working in the same library and in the same package that they would be intimate enough with the code base to not access package scope members from outside the class without a reasonable design debate. It is still a tough call. It would be nice to have a friend class type of designation for this type of instance so that my unit test code code access my private code, but not be accessed by other classes. I have just sealed my jar files in this project through maven:

    
        
            
                org.apache.maven.plugins
                maven-jar-plugin
                2.3.2
                
                    
                        true
                        
                            true
                        
                        
                            true
                        
                    
                
            
    

    Now I am debating about pulling out a bunch of reflection code in my unit tests.

    I am still on the edge of the debate. Package scope is really quite open, especially if you have many developers bouncing around the code base.

提交回复
热议问题