Download Lecture 3 this static Static attributes Static methods The Object Class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
this
Lecture 3
• Calling other constructors from a constructor in
the same class may be done using
this(argument list)
• this must be the first statement in the
constructor.
• This is also the name of a special reference
available in all non-static methods. It refers to the
object whose method is being executed.
• Generics
• Enumerates
• More Java
45
static
46
Static attributes
• Normal attributes have memory reserved
once for each object
• static makes the variable common to all
objects in the class (there actually don’t
have to be any objects at all for the variable
to exist)
• Attributes and methods can be declared
static
• Methods and attributes will then associate
with the class rather than the objects
• Should be used only when necessary since
it is a step away from the object oriented
way of programming.
private static int count;
• Can be accessed using the class name
ClassName.count++;
47
Static methods
48
The Object Class
• (Usually) called using class name and not instance variable
• No need to create an object first
• Example: The Math class in java.lang contains
mathematical functions
Math.abs (num)
Math.sqrt (num)
Math.random () -- random numbers in the interval [0.0…1.0)
• Static methods may only reference other static methods and static attributes
(without first creating an object)
49
• A class called Object is defined in the
java.lang package of the Java standard class
library
• All classes are derived from the Object class
• If a class is not explicitly defined to be the child of
an existing class, it is assumed to be the child of
the Object class
• Therefore, the Object class is the ultimate root
of all class hierarchies
50
1
Object Methods continued
Object methods
• void notify()
– Wakes up a single thread that is waiting on this object's monitor.
• void notifyAll()
– Wakes up all threads that are waiting on this object's monitor.
• String toString()
– Returns a string representation of the object.
• void wait()
void wait(long timeout)
void wait(long timeout, int nanos)
– Causes current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object, or some
other thread interrupts the current thread, or a certain amount of
time has elapsed.
• protected Object clone()
– Creates and returns a copy of this object.
• boolean equals(Object obj)
– Indicates whether some other object is "equal to" this one.
• protected void finalize()
– Called by the garbage collector on an object when garbage
collection determines that there are no more references to the
object.
• Class getClass()
– Returns the runtime class of an object.
• int hashCode()
– Returns a hash code value for the object.
51
Autoboxing/Unboxing
52
Type-safe enum
• Automaticly wraps primitive types into wrapper
classes and back when necessary
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY }
//used
Day today = Day.WEDNESDAY;
This is legal since Java 1.5:
Integer x = 6;
//6 is boxed
Integer y = 2*x + 3; //x is unboxed, 15 is boxed
switch(today){
case SUNDAY:
break;
//…
}
• Enums are objects and there are some methods available for them:
ordinal - get the integer associated with this value
name - get the name of the object (same as the identifier that identifies the
value)
53
Defining Generics
54
Nested Classes
public class GenericStack<T> {
private T[] data;
private int numElem;
public GenericStack() {
data = (T[])new Object[MIN_SIZE];
numElem=0;
}
• In addition to containing data and methods, a class
can contain other classes
• A class declared within another class is called a
nested class
Enclo sing Class
public T pop(){ … }
public void push(T item) { … }
Nested Class
}
55
56
2
Nested Classes
Nested Classes
• A nested class has access to the variables and
methods of the enclosing class, even if they are
declared private
• In certain situations this makes the
implementation of the classes easier because they
can share information easily
• Furthermore, the nested class can be protected by
the enclosing class from external use
• This is a special relationship and should be used
with care
• A nested class produces a separate bytecode file
• If a nested class called Inside is declared in an
outer class called Outside, two bytecode files are
produced:
Outside.class
Outside$Inside.class
• Nested classes can be declared as static, in which
case they cannot refer to instance variables or
methods
• An instance of an inner class can exist only within
an instance of an enclosing class
57
Packages 2
Packages
• We may define new packages
• A Java package is a collection of classes
• The packages can be used to group classes that is
similar or depend on each other together
• The import statement makes the content of the
package available to a class in another package.
import packagename.classname;
import packagename.*;
58
package packagename;
• The Package statement must be the first statement in a
file
• All the classes in the file will be added to the package
• There may be only one package statement in each file
• The package name could be the same as the directory were
the files are stored
• There is a environment variable CLASSPATH that defines
where the system will look for packages
• In case of name collisions class names must be fully
specified
single class
All classes
59
60
3