• Post Reply Bookmark Topic Watch Topic
  • New Topic

Intention of static inner class  RSS feed

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using the static inner class, we can access the private and protected members of the enclosing class. It helps to test the enclosing class. Read it here Is that the intention of the static inner class?

Other thing, what is white-box test of the class and black - box test of the class? Somebody help Please!
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you have that right. A static class, being static, has no access to instance members of the enclosing class, public, private, or otherwise. You can really think of it as a top level class that for organizational reasons is enclosed in another top level class. That is, it's barely an inner class at all.

So, why would you do that? Well, we often use static inner classes for a composite ID of our Hibernate entities. So, let's say there's a Person entity that is keyed by first name and last name. (Hey, relax, it's just an example.) I can construct a key object with: new Person.Id("Greg", "Charles") and use that to search for myself in the database. Could I have the key in a separate class in the same file? No, then it wouldn't be public. Could I have it in a separate class in a different file? Yes, that's fine. It's just a matter of how you think the code looks better.

For your second question, black-box testing assumes you know the only the expected outputs for any given inputs of a test, and all you can do is confirm that your system fulfills this contract. With white-box testing, you understand some or all of the implementation of your system and you use that knowledge to guide your testing.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:I don't think you have that right. A static class, being static, has no access to instance members of the enclosing class, public, private, or otherwise.

Mmm, it does have special access that other classes don't have, even to instance members. It just needs an instance explicitly provided. For example:

The showX() method needs to return t.x rather than x, because there is no associated "this" instance of TopLevel. The point though is that you couldn't do this from another class outside of TopLevel, since x is private. So the Nested class does have access to the private instance variable x; it just needs to specify the instance used to access it.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Note I'm not saying this is a primary purpose of nested classes - but it is a special capability they have, that is sometimes useful.)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!