The Java 2 Sofware Develpment Kit version 1.1 and the documentation
for the Java 2 Sofware Develpment Kit version 1.1 is free.
If you don't have the Java 2 Sofware Develpment Kit version 1.1 then
download (use a program named Go!zilla to do this. Get Go!zilla from http://www.gozilla.com)
the installation program for it from here...
The Java 2 Sofware Development Kit contains, all the tools you need to compile and run Java programs, sample Java source code, and sample Java programs which you can run.
Install the Java 2 SDK version 1.1 on your computer by running the instalation
program.
Once you have installed the Java 2 SDK version 1.1 on your comppter
you should install the API documentation which you can download here...
The documentation is in HTML format.
A Java source code file must have the file extension of .java
A Java class file must have the file extension of .class
You compile Java source code files into Java class files with a program
named javac.
To compile a java source coe file, go to the MS-DOS Command Prompt,
cd to the directory the java source
code file is in, type javac MyJavaSourceCodeFile.java
and then press the Enter key on the keyboard of your computer.
In order to run Java class files you use a program named java.
To run a Java class file named MyJavaClassFile.class then you type
java MyJavaClassFile at the
MS-DOS Command Prompt, and then you press the Enter key on the keyboard
of your computer.
No doubt you will have heard of Java applets. Applets are Java programs
which are run by a World Wide Web Browser such as Netscape Navigator. Applets
are covered later in this guide. Some Java programs are both applets and
applications.
Some Java programs are applications and not applets. Some Java programs
are applets and not applications.
java cannot run Java programs which are not applications. No World
Wide Web Browser can run a Java program which is not
a applet.
Here is an example of a class...
class MyClass {
}
The name of the class is MyClass.
If you were to instantiate an object of class Accord then you would
make an Accord.
You would probably not do this, since Accords are made in a factory.
If you wanted to instantiate an object of class Accord which had only
2 doors
and no sunroof and was colored black then you would write this in Java...
boolean 2_DOOR = true;
boolean SUNROOF = false;
Accord myAccord = new Accord(color.black, 2_DOOR, SUNROOF);
or you cold create the myAccord this way...
Accord myAccord = new Accord(color.black, true, false);
Here is how you write a constructor...
class MyClass {
MyClass() {
//some code goes here.
}
}
A constructor should initialize the object and do nothing else except
that.
See this page for more:
A car can do various things. It can accelerate, it can decelerate, it
can brake, it can
change gears, it can turn left, it can turn right, it can turn on its'
headlights, etc.
How the car does these things will be defined in the class definition
of that car.
The things that a car can do are named methods.
In the class definition there will be a method named accelerate, another
named
decelerate, another named brake, another named changeGear, another
named
turnLeft, another named turnRight, another named turnOnHeadlights.
If you wanted to turn your Accord right then you would write this in
Java...
myAccord.turnRight();
There are certain items of information about a car at any one time.
For example, the
speed of the car, the tire pressure, how much fuel is in the gas tank,
what gear the car
is in, etc. In object orientated programming these items of information
are known as
variables.
A constructor is a method in a class which creates an object of that
class. If you
don't define a constructor method in your class definition then you
can still create
an object of that class by doing the following:
MyClass myObject = new myClass();
Even if you don't define a constructor method you still get a constructor
method.
If you define a constructor method which does take 1 or more arguments
then you will
not get a constructor method that takes 0 arguments. Constructor methods
are usually
know as constructors.
Variables in a class are known as fields.
There are 2 types of variables. Variables of the first type are known
as instance variables.
Variables of the second type are known as class variables.
There can be more than one object of class HondaAccord in exsistence.
So there will be more than one copy of some variables in existence.
For example every
object of class HondaAccord will have a variable named currentfuellevel
because it is
possible that one object of class HondaAccord will have a different
amount of fuel in
its' gas tank to another object of class HondaAccord.
//Every object of class HondaAccord will have 1 steering wheel. Let
us assume that there
//is a variable named
Every object of class HondaAccord will either be allowed to be used
in the Republic of
Muntak or not. So every object of class HondaAccord will have a variable
named
allowedinMuntak. If allowedinMuntak is true then any HondaAccord may
be used in
the Republic of Muntak. If allowedinMuntak is false then no HondaAccord
may be used
in the Republic of Muntak. No matter what the color of the object,
no matter how many
doors the object has, if no object of class HondaAccord is allowed
to be used in the
Republic of Muntak then the variable allowedInMuntak will be false
for every object
of class HondaAccord. The variable allowedInMuntak is a class variable.
If a variable will always have the same value for every object then
that variable is a
class variable.
You define instance variables this way:
int myInstanceVariable 55;
You define class variables this way:
static int myClassVariable 33;
When you see a class cannot be instantiated it means that no object
can be
created from this class.
If you cant instantiate an object from a certain class then that class
is an
abstract class.
If you do this...
AbstractClass abstractClass = new AbstractClass();
you will get an error when you compile the Java source code file using
javac
or any other Java compiler.
The point of having a class that you can't create an object with, is
that any
subclass of that class can implement the methods of that class.
An abstract class can provide the programmer with a list of method
names that the programmer can use.
For example...
//This is the 1st line of the Java source code file named Gretings.java
public class Greetings {
public static void main(String args[]) {
EnglishVersion englishVersion
= new EnglishVersion();
englishVersion.printGreeting();
SpanishVersion spanishVersion
= new SpanishVersion();
spanishVersion.printGreeting();
ItalianVersion italianVersion
= new ItalianVersion();
italianVersion.printGreeting(
);
}
}
abstract class FooBar {
void printGreeting() {};
}
class EnglishVersion extends FooBar {
void printGreeting() {
System.out.println("Hello.");
}
}
class SpanishVersion extends FooBar {
void printGreeting() {
System.out.println("Ola.");
}
}
class ItalianVersion extends FooBar {
void printGreeting() {
System.out.println("Bon
giorno. :-)" );
}
}
//This is the last line of the Java source code file named Greetings.java
This is what the program named Greetings prints on the screen
after you run it with java (by typing java Greetings )...
Hello.
Ola.
Bon giorno. :-)
The is is an unimplemented method...
public void myMethod (int parameter1) {
};
In the above piece of code, the word public is a access specifier.
See the section entitled "Scope".
In the above piece of code, the word void is a return type. Every method
has a return type. The return type of this method is void, so if you have
the following line of code in a Java source code file and attempt to compile
that Java source code
file with javac you will get an error message and javac will not compile
the .java file into a .class file.
int x;
x = myMethod(88);
This is an implemented method...
public void myImplementedMethod(int parameter1) {
String myString = new String();
(Integer)parameter1;
myString = parameter1.toString();
System.out.println( myString);
}
This is how you declare a variable...
String myString;
This is how you initialize a variable...
myString = new String("Hello. How are you today?");
i = 88;
myFloat = 456987F;
anotherFloat = 43.8945f;
class myClass {
int x;
this.x = 88;
}
is the same as writing the following...
class myClass {
int x;
myClass.x = 88;
}
You can pass in a parameter to a method which has the same name as a
variable of the class the method
belongs to. You can use the this keyword to refer to the hidden variable,
The this keyword is used when a
super is also a keyword. Instead of writing the name of the classes
immeadiate superclass
you can write super.
You use the this keyword as in the following example...
class MyClass {
int x;
int y;
public void myMethod(int x ) {
this.x = x
y = x;
}
}
If you had the following code in a Java source code file and attempted
to compile it with javac then javac
would tell you that you had an error in the code and javac would not
compile the code into a class file.
class MyClass {
int x;
public void myMethod(int x ) {
x = x
}
}
You must have this method in the class of a Java applciation...
public static void main(String args[]) {
//The code that you want your program to do goes here.
}
How do you decide what to name a class?
//start of Java source code file named Class1.java
public class Class1 {
public static void main(String
args[]) {
Class2
class2 = new Class2();
System.out.println("Printed
by Class1");
}
}
class Class2 {
public static void main(String
args[]) {
int x = 1;
System.out.println(Printed by Class2");
}
}
//end of Java source code file named Class1.java
To compile and run Class1.java, type the folowing at the MS-DOS Command
Prompt...
javac Class1.java
java Class1
The following text will apear on the screen of your computer...
Printed by Class1
By typing javac Class1.java you also compile another class file named
Class2.class
If you type java Class2 at the MS-DOS Command Prompt then nothing will
happen because Class2 is not a public class.
If Class2 was a private class then javac would display a error message
on the screen of your computer and not compile
Class2 into a Java class file. To make Class2 a private class put the
word private before the word class in this line...
class Class2 {
No class may be declared to be a private class.
For example, this class is declared to be a private class...
private class APrivateClass {
}
If you compile it by typing javac APrivateClass.java then you get an
error message and javac does not compile the class.
//start of the Java source code file named Example33.java
public class Example33 {
}
private class Class331 {
}
//end of the Java source code file named Example33.java
If you attempt to compile Example33.java into a Java class file with
javac then you will get a error message and javac will not
compile Example33.java into a Java class file.
//strat of the Java source code file named Example44.java
public class Class44 {
}
//last line of text of the Java source code file named Example44.java
If you attempt to compile Example44.java into a java class file then
you will get an error message from javac because the name
of the Java source code file is different from the name of the class
which is defined in the Java source code file.
Remove the word public from the 2nd line of the java source code file
named Example44.java.
Type javac Example44.java
javac will compile Class44 into a class file despite the fact that
Class44 is defined in a Java source code file named Example44.java.
You can only have 1 public class defined in any Java source code file.
If you have a public class defined in a Java source code file then the
name of the public class must be the same as the name of
the Java source code file apart from the file extension .java
So if you have a public class defined in a Java source code file, and
the name of the public class is MyPublicClass,
then the name of the Java source code file must be MyPublicClass.java.
If you have a publci class defined in a Java source code file, and the
name of the public class is JumpingText, then the name of the
Java source code file must be JumpingText.java.
So we can define a abstract class named Car and have our HondaAccord
class be a subclass of that class.
abstract class Car {
boolean headlightsOn;
boolean isReversing;
public void turnRight() {}
public void turnLeft() {}
}
public class HondaAccord extends Car {
public void turnRight() {
//method implemented here
}
public void turnLeft() {
//method implemented here
}
}
class SuperClass [
public int superClassMethod(int x) {
return x++;
}
}
class SubClass extends SuperClass {
public int subClassMethod(int y ) {
return y--;
}
}
public class ExampleClass {
public static void main(String args[]) {
SubClass
mySubClass = new SubClass();
int z;
z = mySubClass.superClassMethod(8);
//the
value of z is now 9 because 8 + 1 = 9.
}
}
You define an interface this way...
interface NameOfInterface {
int i;
void myMethod1(int j);
}
Here is how to name a package.
Take the name of your domain name and reverse the order of the words
in the domain name.
So if you domain name is www.ibm.com then the first 2 words of every
package you create will be com.ibm.
You should not have www in your package name because it is not part
of your domain name.
Then decide on a simple name for your package. For example, hello.
To put a Java source code file into a certain package then put the
package statement in the Java source code file. Every .class file created
from the types defined in this Java source code file will belong to that
package.
Example...
package com.ibm.hello;
class A {
}
class B {
}
The qualified name of the class A is com.ibm.hello.A
The qualifeid name fo the class B is com.ibm.hello.B
The simple name of the class A is A.
The simple name of the class B is B.
The class A is in the package whose name is com.ibm.hello
The class B is in the package whose name is com.ibm.hello
Send the writer of this guide feedback.
In the same way, classes have simple names and qualified names.
The simple name of the class Introspector is Introspector.
The qualifed name of the class Introspector is java.beans.Introspector
.
When you are writing the source code for a package named com.ibm, do the following...
The package named com.ibm.airplane has nothing to do with the package named com.ibm.airplane.wing. com.ibm.airplane.wing is not a subpackage of com.ibm.airplane, even though the source code files for the members of com.ibm.airplane.wing are in the folder named wing in the directory named airplane.
A package can not have a package as a member. A package can only have 2 kinds of members. The 1st kind of members are classes. The 2nd kind of members are interfaces.
If you use an import statement in your source code file then you do
not have to use the qualified name of the class.
For example...
import java.beans.Introspector;
class IntroClass {
Introspector i = new Introspector();
}
will compile ok, but
class IntroClass {
Introspector i = new Introspector();
}
will not compile ok. The compiler will give you an error message, because
there it does not know what class you are referring to when you use the
word Introspector.
You can import all the classes in a particular package if you want.
For example...
import java.beans.*;
will import all the classes and interfaces in the package named java.beans
into the Java source code file. You can use the simple
name of any class or interface in the package named java.beans.
The import statement must come after the package statement (if there is a package statement) and before any class declerations or interface declerations in the Java source code file.
The package java.lang is automatically imported into every Java source
code file.
For example...
class Ab {
Integer i = new Integer(88);
}
will compile ok, as will
import java.lang.*;
class Abb {
Integer i = new Integer(88);
}
You cannot import 2 classes or interfaces into a Java souce code file
if they both have the same simple name. You can import only one class or
interface and you will have to refer to the other using its' qualified
name.
public class ScopeExample {
public static void main(Strings args[])
{
FooClass myFooClass = new FooClass();
int x = myFooClass.privateVariableOfFooClass;
// at this point, the value of x will not be 9. When you compile this code(bytypeing
// javac ScopeExample.java at the MS-DOS Command Promt), you will get an
// error message.
}
}
class FooClass {
private privateVariableOfFooClass = 9;
}
If a variable is private in 1 class yhen you cna't assign the value
of that varialbe to any
other variable in any other class.
If a method is declared as private in 1 class then you can't use that method in any other class.
cursor at end of top line, type bak space, it is as if yoo mooved the cursor to before the word and typed enter.
Every variable defined or declared in a class is a member of that class.
Every method defined or declared in a class is a member of that class.
Every constructor defined or declared in a class is a member of that
class.
If a member is declared to be private then the following statements
are true...
You can't access that member from any other class except the class
that that member was defined in.
You can access that member from any method of the class that that member
was defined in.
Look at the following code
//1st line of the Java source code file named ExceptionDemo.java
public class ExceptionDemo {
public static void main(String args[]) {
String[] myString = new String[10];
myString[90] = "hello";
}
}
//last line of the Java source code file named ExceptionDemo.java
public class Foo {
public static void main(String args[])
{
try {
throwException(33);
} catch (ParameterIsBiggerThan10Exception
e) {
System.go.println( e.getMessage() );
} finally {
System.out.println("this gets printed whether x is bigger than 10 or not");
}
}
int throwException (int x) throws ParameterIsBiggerThan10Exception
{
if (x > 10) throw new ParameterIsBiggerThan10Exception();
}
}
class ParameterIsBiggerThan10Exception extends java.lang.Throwable
{
public String getMessage() {
return ("ParameterIsBiggerThan10Exception
was thrown");
}
}
So you create a array this way...
Class[] arrayName = new Class[how many elements you want your array
to have];
What is the number of the last element of an array in Java.
The number of the first element of myStringArray is 0.
The number of the last elemtent of myStringArray is 19, not 20.
The concatation operator is +
String carString = new String( "car" );
String sentenceString;
sentenceString = "My " + carString + " is red.";
When you use 2 numeric types with the + operator, the + operator changes
from a concatentation operator to a plus sign.
So the following code is ok...
System.out.println(5+325);
//This is a comment.
int x = 8; // This is a comment. int x = 8; is not a comment
//This is a comment.
You can have a public method in one class with the same method definition
as a public method in another class.
For example, the following is legal...
//1st line of Java source code file named Tap.java
class Hello {
public int sayIt (int x) {
System.out.println("hi");
return 55;
}
}
class GoodBye {
public int sayIt (int x) {
System.out.println("hi");
return 55;
}
}
//last line of Java source code file named Tap.java
The following is ok, whether or not Hello.java and GoodBye.java are
both in the same directory
//1st line of Java source code file named Hello.java
public class Hello {
public int sayIt (int x) {
System.out.println("hi");
return 55;
}
}
//1st line of Java source code file named Hello.java
//1st line of Java source code file named GoodBye.java
public class GoodBye {
public int sayIt (int x) {
System.out.println("hi");
return 55;
}
}
//last line of Java source code file named GoodBye.java
public class Example333 {
int Method1(int x) {
return
x;
}
int method1(int x) { // not ok
return x;
}
//ok
int method1(double x) {
return
44;
}
//ok
int method1(int x, int y) {
return y;
}
int method1(int y, int x)
double method1(int x) {
return 1.2;
}
}
If you try to compile the above source code file then you will get
an erro.
You can have 2 or more method definitions in 1 class as long as those
methods do not have the same
You can have 2 or more method definitions in 1 class as long as what
is in the parentheses part of the method definition is different.
return_type method_Name ( parentheses_part ) {
//code goes
here
}
It does not matter what the return type of the method is.
If you compile the source code below you will get an error message
despite the fact that method1 has a different return type
from method2.
public class Example666 {
int method1( int x, int y)
{
return x;
}
void method1(int x, int y)
{
System.out.println("hello htree.");
}
}
If you compile the above source code, then javac gives you the following error message...
Example666.java:5: method1(int,int) is already defined in Example666
void method1(int x, int y) {
^
1 error
If you compile the following piece of code then you will not get an error message from javac.
//1st line of text of Java source code file named MyClass.java
public class MyClass {
int method1(int x, int y)
{
return 44;
}
}
class SubClassOfMyClass extends MyClass {
int method1(int xxx, int hello)
{
return
34;
}
}
// last line of text of Java source code file named MyClass.java
If you compile the following piece of code then you will get an error
message from javac.
//1st line of text of Java source code file named MyClass.java
public class MyClass {
int method1(int x, int y)
{
return 44;
}
}
class SubClassOfMyClass extends MyClass {
double method1(int xxx, int hello)
{
return
34.0;
}
}
// last line of text of Java source code file named MyClass.java
This is the error message you get from javac if you attempt
to compile the source code file
named MyClass.java into a class file...
MyClass.java:9: method1(int,int) in SubClassOfMyClass cannot override
method1(in
t,int) in MyClass; attempting to use incompatible return type
found : double
required: int
double method1(int xxx, int hello)
{
^
1 error
//first line of the Java source code file named Example22.java
public class Example22 extends JFrame {
Example22 {
super("My JFrame");
}
}
//last line of the Java source code file named Example22.java
No Java class can be a subclass of more than 1 class. Instead of writing
super("My JFrame");
you could write
JFrame("My JFrame");
Wherever you see the keyword super, you can replace it with the name of the superclass of the class in whose class definition the keyword super appears.
So you could write the class definition for the class named Example22 in the following way...
//first line of the Java source code file named Example22.java
public class Example22 extends JFrame {
Example22 {
JFrame("My JFrame");
}
}
//last line of the Java source code file named Example22.java
...and the class file that javac compiled from the version
of the Java source code file named Example22.java
which is shown just above, would be exactly the same as the class file
that javac compiled from
the Java source code file named Example22.java which
had super instead of JFrame.
Here is another example..
//1zt line of the Java source code file named ExampleYab.java
class
To find out wheter a certain object is an instance of a certain class,
you can use the instanceof keyword.
Look at the following piece of code...
if ( johnsHondaAccord instanceof HondaAccord )
{
System.out.println( "The object named johnsHondaAccord
is an instance of the class named HondaAccord");
}
If an object (lets name this object "myObject") is an instance of a
certain class (lets name this class "Class1"),
and Class1 is a subclass of another class (lets name this class "Class2"),
then myObject is an instance of Class 2.
If Class2 is a subclass of yet another class (lets name this class
"Class3"), then myObject is an instance of Class3.
yipee is a object reference