Hi, Generally this scenario, applies for a static method.
A static method, by definition, exists and is associated in a per-class manner, rather than an instantiated object. So for such a method, there is a unique copy per class which can be accessed using: CLASSNAME.METHODNAME(); In fact, we've many such examples within Java 6 language API. You might want to refer: "Class Methods" section of the page: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html Pratik Saptarshi B.Tech (*Mechatronics*) On Sun, Mar 11, 2012 at 9:51 PM, Te <[email protected]> wrote: > In the exercise 5, Sorting-Comparator project, there are 2 java > classes created. > > The lesson has following codes: > // Get String Comparator object and sort the list > Comparator comp = MyComparators.stringComparator(); > Collections.sort(a1, comp); > > > My question is since MyComparators is a class, how come I don't need > to create an instance for that before using it's method? > > I usually have to create an instance first: > MyComparators mc = new MyComparators() ; > mc.stringComparator() > > Please advise. Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "JPassion.com: Java Programming" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/jpassion_java?hl=en. > > -- You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jpassion_java?hl=en.
