Get Java programming services or web site design from lowd.
Java Rules
Copyright 1999-2000 by Liam O'Keeffe. All rights reserved.

References for this document

Hardtest applet  http://www.lanw.com/
The Java Tutorial from http://java.sun.com
Marcus Green (mock exam) http://www.marcusgreen.com
The Java FAQ maintained by  Peter Van Der Linden http://www.afu.com.javafaq.txt
The 'event generator' idiom, written by Bill Venners,
http://www.javaworld.com/javaworld/jw-09-1998/jw-09-techniques.html
Java 2 Standard Edition documentation
Java Language Specification

The following is a list of the 36 legal outer class declerations. It does not matter whether you have code within
the classbody or not, the outer class decleration is still the same. Replace classname with the name of
the class. Replace interfacename with the name of the interface. Replace interfacename2 with a list of
of  interfaces. There must never be a comma between the last interface in the list and the opeining brace.
Replace superclassname with the name of a class. superclassname must be different from
classname. No 2 interfaces in the list of interfaces may have the same name.

A class can implement more than 2  interfaces.
A class can extend either no class or 1 class.
It does not matter how many spaces there are between the words as long as there is at least 1 space.

class classname  {}

class classname  implements interfacename {}

class classname  implements interfacename, interfacename2 {}

class classname extends superclassname {}

class classname  extends superclassname implements interfacename {}

class classname extends superclassname implements interfacename, interfacename2 {}

abstract class classname    {}

abstract class classname  implements interfacename {}

abstract class classname   implements interfacename, interfacename2 {}

abstract class classname extends superclassname{}

abstract class classname  extends superclassname implements interfacename {}

abstract class classname extends superclassname implements interfacename, interfacename2 {}

final class classname {}

final class classname implements interfacename {}

final class classname implements interfacename, interfacename2 {}

final class classname extends superclassname  {}

final class classname extends superclassname implements interfacename {}

final class classname extends superclassname implements interfacename, interfacename2 {}

public class classname {}

public class classname implements interfacename {}

public class classname implements interfacename, interfacename2 {}

public class classname extends superclassname {}

public class classname extends superclassname implements interfacename {}

public class classname extends superclassname implements interfacename, interfacename2 {}

public abstract class classname {}

public abstract class classname implements interfacename {}

public abstract class classname implements interfacename, interfacename2 {}

public abstract class classname extends superclassname  {}

public abstract class classname extends superclassname  implements interfacename {}

public abstract class classname extends superclassname implements interfacename, interfacename2 {}

public final class classname    {}

public final class classname implements interfacename {}

public final class classname implements interfacename, interfacename2 {}

public final class classname extends superclassname {}

public final class classname extends superclassname implements interfacename {}

public final class classname extends superclassname implements interfacename, interfacename2 {}

No class can be both a abstract class and a final class.

A class can extend a class in another package.

No method in a inteface decleration can have a method body, empty or non-empty, abstract or non-abstract.

You can have a method decleration in an interface as long as it does not have a method body.

You can't override any method in any interface.

You can't overload any method in any interface.

A constructor may be declared either private, public, package, or protected but it does not have to be.

A method can have the same name as the class it is in.

Every bean class must implement java.io.Serializable and have a constructor which takes no arguments.

This will compile ok but it will not run ok...
public class Gooo {
 public static void main(String args) {
 }
}

A return statement will get you out of a method, no matter where in the method it is.

If one static method is in the same class as another static method, then you can call one method from the ohter and vice versa.

This is a method with a missing method body: void m(int i);

If a method has a missing method body then that method must be declared abstract.

If a class contains a abstract method then that class must be declared to be abstract.

A class can extend an abstract class.

You can not instantiate an abstract class.

A class can not extend a final class.

You can instantiate a final class.

An abstract class can have non-abstract methods which are implemeneted.

A class can be an abstract class even though it has no abstract methods and is not  a subclass of an abstract
class.

A friendly class can have a public method.

If a method is implemented then it can not be an abstract class.

No method can be both a abstract method and a final method.

No local class may be public.

You can have a public inner class inside a frielndly outer class.

No variables defined at the class level of the enclosing method are accessible to a local class.

A checked exception is a descendant of the class Exception.

A unchecked exception is a descendant of the class RuntimeException.

The class RuntimeException is a subclass of the class Exception.

You have to try{...}catch{...} checked exceptions.

You can try{...}catch{...} unchecked exceptions but you do not have to.

It is illegal for the constructor of a class not to call the constructor of the superclass of that class if the superclass has no no-arg constructor.

It is illegal to have a line break in a string literal.

A window which has LayoutManager can never be wider than the width of the component which has the greatest width.

A window which has LayoutManager centers components.

//legal field declerations int a; volatile int b; transient int c; transient volatile int d; static int e; static volatile int f; static transient int g; static transient volatile int i; final int j=0; final transient int l=0; final static int n=0; final static transient int p=0; //illegal field declerations final static volatile int o; final transient volatile int m; final volatile int k; final static transient volatile int q; transient final static volatile int r; final int a1; final transient int a2; final static int a3; final static transient int p;

You do not create x[] just because you create[][]

The [] at the end of the name of an array are part of name of the type. You have a class named Point, a class named HelloWorld, an interface named Runnable, an array named x[][], an array named x[], an array named x[][][].

If a class implements a certain interface then any subclass of that class will be an instance of that interface.

The only thing that matters to the signature of a method is the name of the method, the number of parameters to the method, the types of those methods, and the order of those parameters.

If a case clasuse in a switch statement does not contain a break statement then the next case clause will
be executed, whether the next case clause is a default clause or not.

The default clause does not have to be the last clause in a switch statement.

In the switch statement, you can think of the default clause this way: case any_other_value:

In a class declaration, you can only have declerations or instantiations of an object outside of method declerations.

You can not use a for statement, switch statement, if/else statement,
or call any mehtods outside of a method body.

If a method takes a byte as a parameter then you can't pass a array of ints as that parameter to the method.

In the following, the variable x is of type a and the variable y is of type b...
x = (b)y;
A conversion of a variable of type b to a variable of type a has occurred.
A conversion of a variable from type b to a variable of type a has occurred.

If an abstract class contains 1 or more abstract methods then that class must be declared to be abstract.

An abstract class contains abstract methods this way: abstract returnType methodName (parameterList);

A class does not have to override a method in one of its ancestor classes to be able to use that method.???

"" is for strings '' is for chars MyArray[] is a class MyArray is a value

If you call a synchronized method of an object then you lock that object.

The only thing you can do in a class declaration and outside a method declaration is create new variables.

You can assign a less accurate type to a more accurate type.

You can not assign a more accurate type to a less accurate type, unless you use a cast.

System.out.println(1+1); is legal.

System.out.println(1-1); is not legal.

ClassName cn = new ClassName();
cn is a object reference.

Every constructor must have the same name as the class it is in.
 
 

You cant have a static method in a interface decleration.

You can have a static field in a interface decleration.

You can have a method definition inside a local class.

You can not assign a ancestor class object reference to a decendant class object reference,
unless you use a cast.
sub = super

You can access a static variable, from a static method, which is not in that static method.

You can assign a descendant class object reference to a ancestor class object reference.
super = sub

An abstract method must be a member of a absract class and must not be implemented.

If a method has no access specifier then it has an access specifier of friendly,

If a field has no access specifier then it has an access specifier of friendly.

A thread is alive if it has been started and has not yet died.

You declare an array like this: Object myObjectArray = new Object[88];
the following 2 ways are legal ways to declare and array.
int[] x;
int x[];

This is illegal: String[] x = new String[];

a = b;
The value of the variable named b has been assigned to the variable named a.
The value of the varibable named a is now the same as the value of the variable named b.

You can not  start() a thread more than onece.

If you call notify(), notifyAll(), or wait() from a non-synchronized method, you will not get a compile-time error but you will get a runtime-error of type: IllegalMonitorStateException.

If a Thread attempts to yield() to a Thread which has a lower priority than it has then the
CPU of the computer will not start executing the lower priority Thread.

If the 1st digit of a number is 0 then the base of that number is 8.

You cannot have uninitialized fields in an interface decleration.

You cannot have private members in an interface decleration.

If the first 2 charachters of a number are 0x then the base of that number is 16.

If the first 2 charachters of a number are 0X then the base of that number is 16.

The 2 digits of the binary number system are 0 1

The 10 digits of the decimal number system are 0 1 2 3 4 5 6 7 8 9

The 8 digits of the octal number system are: 0 1 2 3 4 5 6 7.

The 16 digits of the hexadecimal number system are 0 1 2 3 4 5 6 7 8 9 a b c d e f

Whenever you call the constructor of a subclass,
the constructor of the highest class in the class hierarchy will be executed before the constructor of the subclass
then the 2nd highest class in the class hierarchy, and so on, until the constructor of the subclass is executed.

If a class implements a interface then all subclasses of that class implement that interface.

If a member in a subclass overrides a member in a superclass then you cannot give that member less scope
than the member in the superclass.

If this(...) is called in a constructor then it must be the 1st method that is called and be called only once.

If super(...) is called in a constructor then it must be the 1st method that is called and be called only once.

A class cannot extend a class which has the same disambiguated name as it.

A disambiguated name is the full name of a class.

SuperClass x = new SubClassConstructorCall(...);
The Super Class default constuctor will be called. and the methods called from x will be the methods in the super class of x,
the variables called from x will be the sub class variables.

Variables defined outside of any method body get a default value if declared but not initialized. Example: int i;

Members defined inside of a method body do not get a default value if declared but not initialized. Example: int i;

The only exceptions you have to catch are checked exceptions.

You can't call a method which is in a non-public class not in the same package as the class from which you call the method.
method1() is in Class1. Class1 is a different package from Class2. Class1 is non-public. You can't call method1() from
Class2.

If a method thorws a exception and that exception is declared in it's signature, then you have to try{}...catch{}
it when you use that method.

If a method or constructor takes a parameter which is of a certain class, then you can pass any object which is a subclass of that class to the method or constructor.

Method declerations in an interface do not have any braces { }.

You can't define a variable more than once in the same method.

Any non-null variable is not boolean true.

If a boolean variable is created without the new keyword then you can't use the equals() method on that variable.

You can use the equals() method to compare 2 Strings whether those 2 Strings were created with or without the new
keyword.

== compares the addresses of the 2 Objects.

equals() compares the values of the 2 Objects.

You can have a variable named x of a certain type in one method in a certain class,
and a variable named x of the same type  in another method in the same class.

If you break out of a loop, then the variable that loop used as a loop counter
is dereferenced and does not exist anymore.

You can't break from one method to another method.

You can't continue from one method to another method.

After a loop completes, the variable that was used as a loop counter by the loop no longer exists.

A system command is a (for example) MS-DOS command such as (for example) cd mydir.

In a Java source code file, any package statements in it must be before  any import statements or any
class definitions.

You can't declare any outer class to be a static class.

You can't declare any outer class to be a private class

You can declare a inner class to be a static class.

You can declare a inner class to be a private class.

In a Java source code file, any import statements must come before any class definitions.

In a Java source code file, comments may come before a package statement.

If a method throws a exception and that exception is not declared in it's signature,
but in it's method body,
(for example: throw new IOException();    )
then you don't have to try{}...catch{} that exception when you use the method, asuming
that exception is a subclass of another exception which the method does throw and is declared in its' method signature. and you try{...} catch (...) {...} that exception.

You do not have to catch runtime exceptions.

Only objects of class java.lang.Throwable or subclasses of java.lang.Throwable can be a parameter to
a try statement.

Only objects that derive from the class Throwable can be thrown.

Each method must catch or specify exceptions that can be thrown by methods that are within the scope of that method.

If one method is within the scope of another method then it can be called by that method,
or a method that is called by a method that is called by that method,
or a method that is called by a method that is called by a method that is called by that method, etc.

You can't have a while loop outside a method definition.

A while loop can take a boolean variable as a parameter.

If any of the classes in any Java source code file is public then that class must have the same name
as the Java source code file.

If all of the classes in any Java source code file is non-public then that class does not have to have the same name as the Java source code file.

If a class is non-public and contains a public method, then that class does not have to have the same
name as the Java source code file it is defined in.

Every method must have 1 and only 1 return type.

The return types are void, any primitive type, any object, any array.

You may not start a line of Java code with a line number, unless your compiler allows you to.

A if statement can only take a boolean parameter.

A switch statement can take either a byte, a short, a int, or a char and nothing else.

If a boolean variable is created, but not initialized, outside of a method definition, then, by default, its value is false.

If a boolean variable is declared like this: boolean aBooleanVariable;  in a method definition then you get a error message from javac.

There are three kinds of exceptions: checked exceptions, run-time exceptions, and errors.

You cast a variable from 1 type to another this way: type1   myType1Variable   (type1)xxxxx;

Every constructor is a method

No construtor returns a value.

Every constructor has the same name as the class that constructor is defined in.

Every class that has a constructor which takes 1 or more parameters does not get a defalut constructor.

The main method of a class should be public.

No inner class can define any static members itself.
If a method has a parameter  whose type is a certain class of object and you call that method with a superclass of that object as a parameter to that method then you will get a error when you compile the source code file with javac.

If a method has a parameter whose type is a certain class of object  then you may call that method with a subclass of that object as a paramether to that method.

No parameter to a method may have the same name as any local variable in that method.

No parameter to a method may have the same name as any parameter to any catch clause in that method

No parameter to a method may have the same name as another parameter to that method.

A parameter to a method may have the same name as a member variable of the class which the that method is a member of.

Every Object is of reference data type.

Every array is of reference data type.

No Object is of primitive data type.

No array is of primitive data type.

The primitive data types are byte, short, int, long, float, double, char, boolean.

The minimum value of a byte is -128.

The maximum value of a byte is 127.

The value of evey int if that int is not initialized is null.
-2147483648 to 2147483647

The minimum value  any short can have is -32768.

The maximum value any short can have is +32767.

The lowest number any int can be is -2147483648

The biggest number any int can be is +2147483647

The minimum value any long can have is 0x8000000000000000L

The maximum value any long can have is 0x7fffffffffffffffL

You initialize a long variable this way: long myLong = 922337203685500000L;

You initialize a double variable this way double myDoubleVariable = 100.0;

double MAX_VALUE = 1.79769313486231570e+308;

double MIN_VALUE = 4.94065645841246544e-324;

If a number starts with 0x then the base of that number is 16 (base 16 is also known as hexadecimal format).

No float may be greater than .

No float may be less than .

You initialize a float variable this way:   float myFloat 1.4f;    float myFloat2 1.45e3f;   float myFloat3 (float)1.2;

float MAX_VALUE = 3.40282346638528860e+38f;

 float MIN_VALUE = 1.40129846432481707e-45f;

Every char is 1 Unicode charachter. char myChar = 'c';

Only legal 1st charachters of identifier: a b c d e f g h i jk l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
$ _

Only legal non-1st charachter in identifier a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
$ _

Every class int is initialized to 0 when it is declared, by default.

Every instance int is not ititialized when it is declared by default.

Every boolean is either true or false.

The last thing any finalize method should do is this: super.finalize();
 
 

The index of the 1st parameter to a java application is 0.

The index of the 2nd parameter to a java application is 1.

The index of the 3rd parameter to a java application is 2.

You may not call a nonstatic method from a static method.

A class may not extend more than 1 superclass.

A class may implement more than 1 interface.

You may not override a final class.

A class must implement all the methods of each interface that it implements.

No class method can access any instance variable of the class in which it was defined in.

You don't have to instantiate a class to access a class member of that class.

You can set the security manager for an applet only once while that applet is running.

You can set the securtity manager for an application only once while that is running.

Every variable that you declare is a instance variable unless you use the static keyword in its' definition.

The file extension of every Java source code file must be .java

The file extension of every Java class file must be .class

If there is a class that has a method named main then the name of the source code file in which the class is in must be the same as the name of the class and have the same capitalization.

The file extension of every policy file is .policy

To compile a aplication or an applet type javac *.java and then press teh enter key on your keyboard.

To run an application type java NameOfClassFile and then press teh enter key on your keyboard.

To run an apple type appletviewer HTMLFileThatContainsApplet.html and then press the enter key on your keyboard.

You can't run a applet with the java program.

You can't run a application with the appletviewer program.

By default, a combobox is not editable.

A non-abstract subclass must override every method in its' superclass which is an abstract method.

If a subclass does not override every method in its' abstract superclass then that subclass must be declared
to be an abstract class.

No sublclass can override a static method in its' superclass.

No subclass can override a final method in its' superclass.

The type of variable a method returns must be the same type as the variable that the method which it
overrides returns.

//The name of the method which a method overrides, must be the same as the method which it overrides.

The name of the method which overrides a method in a superclass, must be the same, as the method
which it overrides.
 
The number of parameters which a method in a subclass takes must be the same as the number of
parameters which the method that that (method in a subclass) overrides takes.

Each parameter which is passed to a method in a subclass which overrides a method in a superclass
must be of the same type as each of the parameters which are passed to the method in the superclass.

You must instantiate a class before you can use any of the instance variables of that class.

You must instantiate a class before you can use any of the instance methods of that class.

This is ok: String myArray[5] = new String; myObject.myMethod(myArray);

There is no such thing as a static class.

No constructor is a method.

There can be a method in a interface decleration which has the same name as the interface.

You cannot have an unimplemented method decleration in a interface decleration.
 

You must put any package statement before any import statements in a Java source code file.

You must put any import statements before any class definitions in a Java source code file.

You must put any package statement before (though not necessarily immeaditly before) any class
definition in a Java source code file.
 
You may not pass a method as a parameter to a method.