File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt abt Inner class using with Collections.
 
Similar Threads
Use of comparator and comparable interface
Sorting ArrayList
High Scores Table
comparable and comparator interface
Printing Object details in for-each loop