Allows 2 classes with the same name (in different packages)
Java has a very large class library;
separated in classes
To use code in a different package:
use import statement(s)
import java.io.*;
import java.applet.Applet;
or just refer to the package and the class
java.io.System.out.println("Hello, world");
Code file names
file and directory names are not arbitrary
java code must be in files named for their class
the class Foo should be in a file named Foo.java
when compiled it creates Foo.class
java directories must be named for their packages
the directory space mirrors the namespace
the idea is that you can look up the compiled class
file for a class just from its name
Object-oriented Programming
Object A software component; consists of variables and methods.
A mechanism for encapsulation.
modularity
information hiding
Message Objects
communicate via message passing. A message consists of an object (the
recipient), a method, and optional parameters.
Java Classes and Objects
Classes have variables (or fields) and
methods.
No global variables, nor global functions.
All methods are defined inside classes
An object is an instance
of a class (like a variable)
Class Declaration
ClassModifiers class ClassName
[extends SuperClass]
[implements Interfaces] {
list of method or variable declarations
}
Class modifiers:
public Accessible everywhere. One public class
allowed per file. The file must be named
ClassName.java
private Only accessible within a file
empty: Accessible within the current class package.
abstract, final
Methods Declaration
MethodModifiers ReturnType Name (ArgumentList) {
body of the method
}
Method modifiers:
public Accessible everywhere.
protected Only accessible within the class or a subclass.
private Only accessible within the class.
empty: Accessible within the current class package.
static, final, synchronized, native, abstract
Field Declarations
FieldModifiers Type VarName;
Field modifiers:
public Accessible everywhere.
protected Only accessible within the class or a subclass.
private Only accessible within the class.
empty: Accessible within the current class package.
static, final, volatile, transient
Combine static and final for a true constant
Java naming conventions
Class and Interface names are capitalized
Reference types, fields, and methods are not
But if they're multiple words, the other words are
capitalized
Example: interestRates; toString
Objects
accessed by "reference" (pointers)
use new to get a new one (but there's no delete)
default value for reference types is null (not NULL)
Object syntax more or less same as C++ (but there
is no -> operator)
Constructors; e.g.
String s = new String ("foo");
access fields and methods with a dot
foo.bar = 5;
accesses the bar
field
foo.baz(0);
accesses the method baz with argument 0.
Examples
A class
public class Point {
private int x, y;
public Point() {
x = 0;
y = 0;
// could do this in the declaration of x and y
}
public Point(int dx, int dy) {
x = dx;
y = dy;
}
public void move(int dx, int dy) {
x += dx; y += dy;
}
}
No public or private sections: keyword
must precede each declaration.
Include method definition within class definition (instead
of just the prototype)
An object: point1
Variables: int x, y;
A method: void move(int dx, int dy)
A message: point1.move(10, 10)
Point point1 = new Point(); // no-arg
Point point2 = new Point(20, 20);
Example: A Stack class
How would we do this with an
array, a linked list??
Example of Reference types
Reference types really are pointers
import java.io.*;
public class foo {
public static void main (String[] s) {
test v1 = new test(1),
v2;
v2 = v1;
v2.setint(3);
v1.printit(); } }
class test {
private int x;
test(int y) { x = y; }
public void printit() {
System.out.print(x); }
public void setint(int z) {
x = z; } }