• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

collections example in scjp5 book!!!!

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the example on page 557 , why we used a static class to implement
Comparator??
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you show the full code??
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the example on page 557 , why we used a static class to implement Comparator??



The code sample creates an instance of the static member type ReSortComparator within the static context of the main method. A static member type such as ReSortComparator can be created within the context of the main method even though no instance of SearchObjArray exists. If you were to delete the static modifier from the declaration of ReSortComparator, then ResortComparator could not be created within the static context of the main method because an enclosing instance of SearchObjArray would not be available.




If you add the static modifier to the declaration of ReSortComparator, then it will compile.
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sir
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class SearchObjArray {
public static void main (String [] args) {
SearchObjArray.ReSortComparator rsc = new SearchObjArray().new ReSortComparator();
}
class ReSortComparator implements Comparator<String> {
public int compare (String a, String b) {
return b.compareTo(a);
}
}
}

???THIS IS COMPILING???
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

???THIS IS COMPILING???



Yes, you provided an enclosing instance of SearchObjArray for your non-static inner class, ReSortComparator.
 
author
Posts: 9014
20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dan,

How are you doing dude?!
 
manny singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
Hey Dan,

How are you doing dude?!



I'm doing fine.

It looks like you've published a few more books since I was last here at the JavaRanch. I like your Java 5 study guide, and I often recommend it to anyone that's interested in Java and the certification exam. I'm sure that the Java 6 version will also be great when it becomes available this summer.
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic