Gosling's Java Book Notes

Consolidate: to do list:

Read chapter 3 (subclasses) and 4 (interface) and read chapter 7 (exceptions)

Make notes

 

 

What is a Java Solution?

Developing java solution to a problem is essentially designing suitable class either from scratch or through inheriatnce. Identifying the requirements and modelling them in a class oriented manner (OOA) is a pre-requiste to good java code development. A well built solution will have few (to avoid straining the garbage collector) cooperating objects of extensible classes that themselves have been derived from existing java classes. Please note that number of object references will not really strain the collector, its the actual objects created using new that will. The basic elements of a class are called its members. Members (who can be classes or object references) can be preceded by modifiers, which modifies the conduct of the member it precedes. Following are all the java modfiers:

Access modifiers (only one can be used)

Other modifiers (all or none can be used after the access modifier in any order. However, for consistency we will use the order static | transient | synchronized | final. Static modifier makes the member a class level member instead of an object level member. Hence, outside the class one should and can refer to it by class.name not objectReference.name. A static field exists even if there is 0 object. Static methods can only acess static fields and static methods. final makes a member unchangeable for the life of that object. A static final makes a member class level and permanent, i.e. a named constant!

static final int pie =3.14;

A silly combination of static method and data can make a java code look very C like (Yuck!). Be caustious about decididng what should be static and what should not be.

What are the class members?

Following are the some of the things that one may see in a java class:

Field: Holds data. The data can be and usually are object references. They can be initialized with a new statement that creates the object and assigns it to the reference variable. Initializers (the statement that performs the initialization) can also invoke methods to perform its task. Initializers can not invoke methods that throw exceptions. The fields are initialized to their defualt values (null for object reference). Then their values are modified if there is an explicit intialization. For example, int i =5; etc. After that one of the constructor(if any) of a class is invoked.

Method: They are the "code" of the older structured paradigm. Each method in java has a signature that identifies it uniquely . This is its name, number and type of its parameters and the associated semantics. Two methods having the same name are actually different if their number of parameters differ or the types of paramemeters differ (or both of course). The compiler chooses the best match. page 125 chapter 5. Inside a method the other members of the class can be rererenced directly with out the use of the "dot' operator. In the case of object.method, it is customary to say that the method has been invoked on the object and the object is called the receiver obejct The public methods of a class (and its associated semantics) establishes its contract. The idea is to have public methods that manipulate (get or set) private data. These data define the property of an object. These methods are called accessors. Accessors methods and the porpeties they define are used by JavaBeans to provide automatic property manipulation system. Another alternative to accessor is using the final modifier. This allows the fields to be set only once . A more detailed discussion of final variables will be found in page 111, chapter 5.

this can not be used to access static members. The most common use of this is passing the current object (pass myself) to another object. Use this only when it is needed, i.e. when a parameter may hide the member.

toString is a special method. If used without parameters and if it returns a String, then it will get implictly called, if the name of the object is used with a + (string concat) operator or in the cases like System.out.println(object) etc.

finalize is a method that is invoked by the garbage collector before reclaiming a dying object. It can of course be invoked by any one else who has access to it. It can be used to do clean up of non java resources that are not claimed by the garabage collector. For example, files. If we have a class that provides services like opening and closing of a file, the user (progarmmer who is using my class) may open files but forget to close them. If I have a finalize method that closes the file, I can guarntee that regardless of the quality of the programmer the files will be closed at least before the object dies. The files are usually automatically closed only when the application exits not when an object dies. So to avoid lots of open files left by a bad progrmmaer, we have to implement a finalize method. In any finalize method I write I must develop a habit of using super.finalize in the finally clause. This should be true even if I am not extending any class! This ensures that regardless of what ever happens in my class, the super's finalize will be exceuted. I am dying but I do not want to leave any bad legacy up the tree!

Resurrection of objects can happen during finalize by making a reference to that object again. but finalize is invoked only once by the collector. So you can only resurrect once! The second time the object will die without executing finalize. If the design really! requres resurrection, object cloing is suggested.

methods can return >1 results

Object someMethod (Obejct localCopyObejectReference) {
      localCopyObejectReference = null;
}

actualObjectReference will still be OK. The state can be changed through the rereference, not the rereference itself. Instead of a pass by reference we have references as pass by values.

Constructor (A variation of method, but not really a method )

They are used when either the fields need to get some information from the object creating code to set its initial values or the initialization is too complex for a simple assignement statement. If no explicit constrcutors are specified, java calls an implicit no-args constructor that does nothing. So essentially, Y x = new Y(); calls a blank constructor.

Constructors have the same name as the class they initialize. However, constructors can be overoladed. The keyword this can be used like this() or whatever the signature is to call a constructor from another constructor. This avoids duplication of code. If there are explicict constrcutors, no- args constructor need to be explicit too. Access modifiers in the constructors are usually blank or public. Other access modifiers can be used to implment restriction on extensibilty. I am not sure why you would do that. One can also use protected to mark constructor that make sense only for subclasses.

Initialization Block: They are codes in a class with just { } . They usually will have a static infront of them. Order of initialization is first to last, i.e. each block (or simple initialization) is run before the next one. static blocks are run when the class is loaded and non static blocks are run when the obeject is created. In the case of a cyclic dependecny, i.e. when two classes invoke each other's method in theier initialization block, the compiler does not detect it, instead, works with as much value possible and then use the default values.

Types (classes and intefaces)

Classes can also be nested and they behave similiar to other members. 51 -54

Chapter 3

extends is the keyword used to create sub classes. All the fields and methods are inherited in the subclass. Except when the members of a super class has private modifier

If Pixel is a subclass of Point then any method that expects a Point as a parameter (probaly to use some of its services) can be can be handed a Pixel. All the Point code can be used by anyone with a Pixel in hand. So Pixel can be used by codes written to work with Pixel or Point. This is polymorphism, because Pixel has multiple forms.

super is used to invoke methods of the super class. It prevents code duplication when we are overriding a method to do all of its parent's stuff + some of its own stuff. The run time system will locate the first super class up the inheritance hieararchy to determine the method to use. Any other references in a polymorphic situation, a method will be used that belongs to the actual object not the type of the object referenece. For example:

Point X =  new Pixel();
X.clear(); //will use Pixel's clear not Point's clear
super.clear(); // will use Point's clear;

If some class is not explicitly extended it is an extension of the Object class.

 

Chapter 4

interface is a design issue can not really see the use for it yet

mechanism is

interface Lookup {

method declaration

constants

}

This can be used by code like Lookup X; This will actually find the code that implments the interface. For example:

class someLookup implements Lookup {

      // will implment all the methods etc.

}

A class of an object is the class itself and all the super classes and the interfaces the class implements. An object can be used polymorphically with both its supertypes and super interfaces, including theier supertypes.

Chapter 5

Chapter 6

Chapter 7 checked exceptions manage error handling. When a method has a throws clause the comipiler checks to make sure that the method does not throw any other exceptions and does have code to throw the exeption it has declared

If the methods that throws exception do not handl them it will generate compile time erors. This is why they are called checked exceptions. An exception is usaully derived from the java class Exception and it has to be derived from Thorwable (which is a super class of Exception). finally clause always gets executed.

try {
} catch (exception type 1 id) {
} catch (exception type 2 id) {
} finally {
};

Chapter 8

Chapter 10

Chapter 11

our package name shoulbe COM.java-emporium.package

implictly a file of classes is an unnamed package