Definitely, the answer is no!
But look at the code below:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class EmployeeSort {
static Comparator<Employee> NAMESORT = new Comparator<Employee>(){
public int compare(Employee e1,Employee e2){
return e1.name.compareTo(e2.name);
}
};
public static void print(List<Employee> empList){
for (Employee o :empList){
System.out.println(o.name);
}
}
public static void main(String[] args) {
List<Employee> empList = new ArrayList<Employee>();
Employee e = new Employee("anil");
Employee e1 = new Employee("babe");
Employee e2 = new Employee("akhil");
empList=Arrays.asList(e,e1,e2);
print(empList);
Collections.sort(empList,NAMESORT);
print(empList);
}
}
How do you explain the code snippet below?
static Comparator<Employee> NAMESORT = new Comparator<Employee>(){
public int compare(Employee e1,Employee e2){
return e1.name.compareTo(e2.name);
}
};
Actually this expression instantiates a new object from an unnamed and previously undefined class, which automatically implements the interface named
Comparator, and automatically extends the class named Object. The class can explicitly implement one, and only one interface, and cannot extend any class other than Object. Once again, the body of the new class is given by classBody.
I got answer from stackoverflow:
http://stackoverflow.com/questions/2978849/can-anybody-explain-the-working-of-following-code?rq=1
More detailed information you can refer to the original article:
http://www.developer.com/java/other/article.php/3300881/The-Essence-of-OOP-using-Java-Anonymous-Classes.htm
分享到:
相关推荐
To use the class inside the jsp page we need to create an object of the class by using the new operator. At last use the instance of the class to access the methods of the java file. Setting Colors ...
What is the difference between an abstract class and an interface? **Answer**: In Java, an abstract class can have both abstract and non-abstract (concrete) methods, whereas an interface can only ...
We can declare data members of type Foo* or Foo&. We can declare (but not define) functions with arguments, and/or return values, of type Foo. (One exception is if an argument Foo or const Foo& has a...
With the formatting interface, you can format cells through a cell object. Fast and efficient support when you have any questions. No runtime fees for the component. Full source code included. We ...
Based on the provided information from "iOS 5 Programming Cookbook" by Vandad Nahavandipoor, we can derive a comprehensive set of knowledge points related to iOS development using Objective-C....
With the formatting interface, you can format cells through a cell object. Fast and efficient support when you have any questi ons. No runtime fees for the component. Full source code included. We ...
For example, you can perform operations with a group of objects as well as with a single object in Object Manager, sort, group and filter the database objects within Object Browser, copy an object ...
For example, a car can be considered an object with properties (color, make, model) and behaviors (drive, stop). ##### 2. In Visual Basic .NET In VB.NET, objects are used extensively. They are the ...
If you're viewing this document online, you can click any of the topics below to link directly to that section. 1. Tutorial tips 2 2. Introducing the JavaMail API 3 3. Reviewing related ...
When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and ...
Putting a great looking site together takes a lot of work, and most Java developers are more interested in creating a great looking object interface than a user interface. JavaServer Pages (JSP) ...
Custom label enhancements - New to Version 5.0, developers can associate a custom label to an image, a tooltip, an HREF or even an object Improved Data Labeling - Developers now have the opportunity ...
buttons, forms, display dialog boxes, and create menus.Chapter 10 provides an introduction to application debugging and error handling.Chapters 11 through 13 provide an introduction to object–...
<END><br>38,ShutDnCtl.zip Shutdown Control v1.5 This has been tested on Windows 9x, me, NT and 2000 operating systems.<END><br>39,Margin.zip With the help of this Program we can Leave space...
Chapter 9, Backend Views – Design the User Interface, covers the web client's View layer, explaining the several types of views in detail and all the elements that can be used to create dynamic and ...
sprite1.zip This is an Excellent example on how to use sprites in your program.<END><br>18 , charcreate.zip This is an example of how to assign "characters" to differant pictureboxes. This ...
ion software can direct tens of thousands of enterprise employees to carry out production tasks in an orderly manner. Just imagine, the power of information software is enough to shake the entire ...
4.9 Automatic Object Spinner............................................79 4.9.1 Script Breakdown..........................................80 ix Contents 4.10 Scene Manager...............................
3. **Swift Programming Language**: An overview of Swift, the recommended language for iOS development, including syntax, data types, control structures, and object-oriented programming concepts. ...