Richard's J2SE Day 5, Creating Classes and Methods

(P112) In Java, a program is made up of a main class and any other classes that are needed to support the main class. These support classes include any of those in Java's class library (String, Math etc)

Defining Classes (P112)
class Ticker {
// body of the class
}


The extends keyword indicates the superclass of a class.
class SportsTicker extends Ticker {
// body of the class
}


Creating Instance and Class Variables
Defining Instance Variables (P112)

Variables are considered instance variables if they are declared outside a method definition and are not modified by the static keyword. By custom, most instance variables are defined right after the first line of the class definition.

class VolcanoRobot extends ScienceRobot {
String status;
int speed;
float temperature;
int power;
}


Class Variables (P113)

Class variables apply to a class as a whole, rather than being stored individually in objects of the class.
The static keyword is used in the class declaration to declare a class variable, as in the following:
static int sum;
static final int maxObjects = 10;


Creating Methods
Defining Methods (P114)

returnType methodName (type1 arg1, type2 arg2, type arg3 ...) {
// body of the method
}


note if this method returns an array object, the array rackets can go after either the returnType of the parameter list.
int[] makeRange ( int lower, int upper ) {
// body of this method
}


(P115) RangeClass.java

The this Keyword (P116)
The this keyword refers to the current object.

Variable Scope and Method Definitions (P116)
When you refer to a variable within a method definition, Java checks for a definition of that variable first in the current scope (which might be a block), next in each outer scope, and finally, in the current method definition. If the variable is not a local variable, Java then checks for a definition that variable as an instance or class variable in the current class. If Java still does not find the variable definition, it searches each superclass in turn.
(P117) ScopeTest.java

Passing Arguments to Methods (P118)
When you call a method with object paramenters, the objects you pass into the body of the method are passed by reference. Whatever you do to the objects inside the method affects the original objects.
Such objects include arrays and all objects that are contained in arrays. When you pass an array into a method and modify its content, the original array is affected.
Primitive types, on the other hand, are passed by value.
(P118) PassByReference.java

Class Methods (P119)

The relationship between class and instance variables is directly comparable to how class and instance methods work.
The lack of static keyword in front of a method name makes it an instance method.

Class methods are available to any instance of the class itself and can be made available to other classes.
Unlike a instance method, a class does not require an instance of the class for its methods to be called.
e.g. Java library class include a class called Math
double root = Math.sqrt(45.0);

The define class method, use the static keyword in front of the method definition, just as you would use static in front of a class variable.
static int max(int arg1, int arg2) {
// body of the method
}


Java supplies wrapper class for each of the base types; for example, Java supplies Integer, Float and Boolean classes.
int count = Integer.parseInt("42");

Creating Java Applications (P121)
Applications => Java programs that run on their own.
Applications are different from applets, which require a Java-enable browser to view them.

The only thing you need in order to make a Java application run, is one class that serves as the starting point for the rest of the Java program => starting point class needs only one thing: a main() method.

The signature for the main() method:
public static void main(String[] arguments) {
// body of method
}


public = method is available to other classes and objects
static = main() is a class method
void = main() method does not return a value
main() takes one paramenter, which is an array of strings

The body of the main() method contains code to start your application, such as initialization of variables or the creation of class instances.

main() is a class method. An instance of the class that holds main() is not created automatically when your program runs. If you want to treat that class as a object, you have to create an instance of it in the main() method.

Java Applications and Command-Line Arguments (P123)
How you pass arguments to Java application varies based on the platform you are running Java on and the development tool you are using.
To pass arguments to a Java program using the SDK, the arguments should be appended to the command line.
e.g.
java EchoArgs Wilhelm Niekro Hough "Tim Wakefield" 49
Note that a space seperates each of the arguments.
(P123) SumAverage.class

Creating Methods with the Same Name, Different Arguments (P125)
Two things differentiate methods with the same name:
- The number of arguments they take
- The data type or objects of each argument
Note: skipped example on pg 127

Constructor Methods (P128)
A constuctor method is a method that is called on an object when it is created.
Unlike other methods, a constructor cannot be called directly. Java does three things when new is used to create an instance of a class
- Allocate memory for the object
- Initialize the object's instance variables, either to initial values or to a default
- Calls the constructor method, which might be one of the several methods

Basic Constuctors Methods (P129)
Constructors look like regular methods, with three basic differences
- always have the same name as the class
- don't have a return type
- cannot return aa value in the method by "return" statement

Calling Another Constructor Method (P129)
You can call the first constructor from inside the body of the second constructor, syntax:
this (arg1, arg2, arg3);

Overloading Constructor Methods (P130)
Constructor methods also can take varying numbers and types of parameters.
Note: skipped example

Overridding Methods (P132)
Call an object's method, Java looks for method in the object's class. If not found, it passes the method up the class hierarchy.
To overide a method, define a method in a subclass with the same signature as a method in a superclass. When the method is called, the subclass method is found and executed instead of the one in the superclass.
Note: skipped example

Overidding Constructors (P134)
Technically, constructor methods cannot be overidden. Because they always have the same name as the current class, new constructor methods are created instead of being inherited.
However, to change how your object is intialized, explicitly call the constructor methods of the superclass and subsequently change whatever variables needed to be changed.

To call a regular method in a superclass, use super.methodname(arguments)
super(arg1, arg2, ...);
Note: skipped example p135

Finalizer Methods (P136)
Finalizer methods are called just before the object is collected for garbage and has its memory reclaimed.
The finalizer method is finalize(). The object class defines a default finalizer method that does nothing. To create a finalizer method for your own class, override the finalize() method using this signature:
protected void finalize() throw Throwable {
super.finalize();
}

Include any cleaning up that you want to do inside the body of that finalize() method.

(P140) Exercise VolcanoRobot.Class