Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 4 Advanced Class Design

Counter anotherInstance = new Counter(); Counter.printCount();

}

}

This program prints

Number of instances created so far is: 2 Number of instances created so far is: 3

Do not confuse a static block with a constructor. A constructor will be invoked when an instance of the class is created, while the static block will be invoked when the program initializes.

Points to Remember

The main() method, where the main execution of the program starts, is always declared static. Why? If it were an instance method, it would be impossible to invoke it. You’d have to start the program to be able to create an instance and then call the method, right?

You cannot override a static method provided in a base class. Why? Based on the instance type, the method call is resolved with runtime polymorphism. Since static methods are associated with a class (and not with an instance), you cannot override static methods, and runtime polymorphism is not possible with static methods.

A static method cannot use the this keyword in its body. Why? Remember that static methods are associated with a class and not an instance. Only instance methods have an implicit reference associated with them; hence class methods do not have a this reference associated with them.

A static method cannot use the super keyword in its body. Why? You use the super keyword for invoking the base class method from the overriding method in the derived class. Since you cannot override static methods, you cannot use the super keyword in its body.

Since static methods cannot access instance variables (non-static variables), they are most suited for utility functions. For example, all methods in the java.util.math library are static.

Calling a static method is considered to be slightly more efficient compared to calling an instance method. This is because the complier need not pass the implicit this object reference while calling a static method, unlike an instance method.

Flavors of Nested Classes

Classes defined within the body of another class (or interface) are known as nested classes. Normally you define a class, which is a top-level class directly belonging to a package. In contrast, nested classes are classes contained within another class or interface.

What is the benefit of creating classes inside another class or interface? There are several benefits. First, you can put related classes together as a single logical group. Second, nested classes can access all class members of the enclosing class, which might be useful in certain cases. Third, nested classes are sometimes useful for specific

purposes. For example, anonymous inner classes are useful for writing simpler event-handling code with AWT/Swing (which is not relevant to the OCPJP 7 exam, and hence not covered in this book). For now, you can take our word for it that nested classes are useful in some situations, so it is worth learning about them.

94

Chapter 4 Advanced Class Design

There are four types or flavors of nested classes in Java:

Static nested class

Inner class

Local inner class

Anonymous inner class

The distinctions among these four flavors are not evident at first sight, and it doesn’t help matters that alternative terms are often substituted for them. To help clarify the confusion, we represent the four flavors of nested classes schematically in Figure 4-1.

 

 

 

class Outer{

 

class Outer{

 

class Inner{

 

//class definition

static class StaticNested{

 

 

}

 

//class definition

 

 

 

}

 

}

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

Static

Non-static

Anonymous

 

Non-local

Static nested

Inner class

(Not possible)

 

 

class

 

 

 

Local

 

 

 

 

(Not possible)

Local inner

Anonymous

 

 

 

class

inner class

 

 

 

 

 

class Outer{

class Outer{

void foo(){

void foo(){

return new Object(){

class LocalInner{

public String toString(){

//class definition

return “anonymous”;

}

}

}

}

}

}

 

 

}

Figure 4-1.  Types of nested classes with examples

Study this figure. A local class is defined within a code block (whether a method, constructor, or initialization block), whereas a non-local class is defined inside a class. A static class is qualified using the static keyword, whereas a non-static class does not use this static keyword with the class definition. In an anonymous class, you don’t provide the name of the class; you just define its body!

As you can see in Figure 4-1, static nested classes are static and non-local, whereas inner classes are non-static and non-local. A non-static and local nested class is a local inner class, and a local and anonymous nested class is an anonymous inner class. A glance back at this figure will serve to refresh your memory of the essential distinctions among the four flavors of nested classes.

Now, let’s discuss each of these four flavors in more detail.

95

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]