Java Classes

General Information

Using Classes

A class can hold static fields and methods. A class can also be a template to create instances, these instances hold (non-static) fields and methods. Instances are created by
new ClassName ()
new ClassName (args)

Arbitrary-Precision Math

A case study in using existing classes. Examples of creating BigInteger and BigDecimal values
BigInteger bi;
bi = BigInteger.valueOf (1);
bi = new BigInteger ("123456789ABCDEF", 16);
BigDecimal bd;
bd = new BigDecimal ("-2.71828")
bd = new BigDecimal ("1.23E-23")
Examples of BigInteger

Classes as data structures

When using a class as a template
fields
methods
  constructors

Simple, complete classes as data structures. Point2D.java, Circle.java, and Line.java.

Also: Polynomial.java, Person.java, and Elephant.java

Classes as simulation. BankAccount.java, Image.java, Aircraft.java

Enumeratation Types

Examples: Main.java, and Card.java.

   enum Direction {
      RD(+2,-1), RU(+2,+1), LU(-2,+1), LD(-2,-1),
      UL(-1,+2), UR(+1,+2), DL(-1,-2), DR(+1,-2);
      private int dx,dy;
      Direction (int dx, int dy) { this.dx=dx; this.dy=dy; }
      public final Square move (Square p) {
         return new Square (p.x+dx,p.y+dy);
      }
   }

Methods (Subprocedures)

Methods can be both static and instance (not static).

Actual Arguments, Formal Parameters

Definitions: actual arguments, formal parameters.
System.out.println ("n=" + n);
(the actual argument is in red)
void method (final int arg) {
   final int s = arg+arg;
}
(the formal parameter is in red)

§ 8.4.1 Formal Parameters.

Examples of these three errors Errors.java,

Overloading

Parameter Passing

Compile Time Checking

[Where does this all belong?] Practically no compiler warnings in Java.

final

See "§ 4.5.4 final Variables" in the Java Language Specification
A variable can be declared final. A final variable may only be assigned to once. It is a compile time error if a final variable is assigned to unless it is definitely unassigned (§16) immediately prior to the assignment.

A blank final is a final variable whose declaration lacks an initializer.

Single assignment

Constructors

Definite Assignment

"Chapter 16: Definite Assignment" in the Java Language Specification
void flow(boolean flag) {
   int k;
   if (flag) k = 3;
   else      k = 4;
   System.out.println (k);  // k is "definitely assigned" before here
}

void flow(boolean flag) {
   int k;
   if (flag)  k = 3;
   if (!flag) k = 4;
   System.out.println (k);  // k is not "definitely assigned" before here
}

Unreachable

"§ 14.20 Unreachable Statements" in the Java Language Specification
It is a compile-time error if a statement cannot be executed because it is unreachable. Every Java compiler must carry out the conservative flow analysis ... to make sure all statements are reachable.

Shadow

Scope.java

Order of Initialization

static blocks, order of initialization [goes else where!]

Bad.java

Three.java

Order.java

Classes and inheritance

Modifiers

Access modifiers

At most one of: The absence of any access modifiers indicates "package" access. See the discussion after packages.

Class modifiers

See § 8.1.1 Class Modifiers

Field modifiers

See § 8.3.1 Field Modifiers

Method modifiers

See § 8.4.3 Method Modifiers

final modifier

To summarize the uses of the final modifier on fields, methods, classes and more: The following usage notes:

Abstract classes

Interfaces

See chapter 9: Interfaces in the Java Language Third, Second Edition

Some important interfaces:

Nested classes

"A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class." --- from chapter 8 Classes in the Java Language Specification, Third Edition.

"A member class is a class whose declaration is directly enclosed in another class or interface declaration." --- from the section Member Type Declarations.

"An inner class is a nested class that is not explicitly or implicitly declared static." --- from the section 8.1.2 Inner Class and Enclosing Instances. --- from the section 8.1.3 Inner Class and Enclosing Instances.


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Fri Sep 16 09:48:44 EDT 2011