jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt abt Inner class using with Collections. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt abt Inner class using with Collections." Watch "Doubt abt Inner class using with Collections." New topic
Author

Doubt abt Inner class using with Collections.

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

The output of above code is

Before Sorting
3 2 4 1
After Sort By Comparable
4 3 2 1
After Sort By Comparator
1 2 3 4

Only doubt i have in above code is that how i can create instance of
inner class(MyComparator)at line 15 without creating instance of outer class(SortTest) class.Because MyComparator is inner class at line 38.

Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
You cannot. But you can convert your inner class MyComparator into a static nested class, and use it as in new SortTest.MyComparator().

By the way, these classes suffer from very high coupling - the tested class cannot exist without the other testing class.
[ August 22, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Then how above code compiles and run with perfectly output.
That is what i want to know.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
It does not compile for me.
The error (on your line 15) is "cannot find symbol - class MyComparator".

Are you sure that you do not have another MyComparator class somewhere?
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Barry Gaunt you are right.
when i compiled same class using NetBeans i got same error.

Thanks
 
Consider Paul's rocket mass heater.
 
subject: Doubt abt Inner class using with Collections.
 
Similar Threads
High Scores Table
Use of comparator and comparable interface
Sorting ArrayList
comparable and comparator interface
Printing Object details in for-each loop