• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the correct way of instantiating static nested class ?  RSS feed

 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Based on this, the proper way of creating a static nested class is


Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();


However I tried the following


I see that both case 1 and case 2 work correctly. So what is the difference in both approaches and which one is preferred over the other and why ?

Also, the information provided in the document, is that a recommendation or the compiler is expected to throw error if static nested class is created without reference to the outer class.

Please advise.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar Raja wrote:I see that both case 1 and case 2 work correctly. So what is the difference in both approaches and which one is preferred over the other and why ?

The second (qualified) one will work in ANY case. The first works, in your case, because the name being created is already known to the compiler (because it's defined in the same .java file that contains the code).

Winston
 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar Raja wrote:Thank you Winston

You're most welcome.

Winston
 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,

In continuation to my question asked in this thread, I have few more questions

Please consider my current design is as follows



Could you please tell me ,if I need to use the reference of outerclass i.e MyMessageListener , while creating PersonComparator or even while creating Person objects. Neither Person nor PersonComparator uses any of the instance variables of MyMessageListener.

Do you see any flaw in my current design ?

Also, even if I had to use the reference of outerclass, say like new MyMessageListener().new PersonComparator(), MyMessageListener() is actually not instantiated by my program at first place. It starts with server startup and will be alive all the time.


For the questions
Ask yourself this: Is your "inner" class dependant on:
(a) the implementation of its outer class? - Not exactly. But the inner classes are used logically by outerclass only. No other class will ever require access to these inner classes.
(b) a specific object of its outer class? No
(c) a broader specification - usually an interface (not strictly necessary)? - No

Since, more or less for all the questions above, the answer seem to be No, so based on your comments. I'm required to use a static inner class and then need to create that with the reference of outer class. But again, since the outerclass is a Listener and I do not create it, do I need to create innerclass as

new MyMessageListener.PersonComparator();
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar Raja wrote:In continuation to my question asked in this thread, I have few more questions

1. In general it's best to UseOneThreadPerQuestion (←click), but I suspect it's too late now. For one thing, nobody will work out your question from the title.
2. Please DontWriteLongLines. I've broken yours up this time.

Do you see any flaw in my current design ?

Yes. What does a Person have to do with a Message? you should only even consider a nested class if the "inner" class only has a meaning within the outer class.

A simple test is this: Does the name Message.Person make any sense?

In your case a Person would simply appear to be class that defines a person, so I'd say that it probably doesn't.

PersonComparator (or rather: Message.PersonComparator) on the other hand, may well be a good candidate for a nested class but, since it doesn't rely on a specific Message, you're probably better off making it static.

Winston
 
Kumar Raja
Ranch Hand
Posts: 550
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback Winston. I will try to work on the modification.
 
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!