Subsections
- Class - the fundamental unit of programming in Java.
- Classes contain the attributes and behaviors of the objects you will create.
- Classes define how an object should be created, how it should be destroyed, and how it should behave during it's existence.
Upon completion of this section, you should be able to:
- Define the difference between a Java class and an object.
- Create a simple Java class.
- Create constructors for your classes.
- Define the behavior of your classes.
- Be able to describe Java's garbage collection process.
- Declare and initialize instance variables.
- Access data members and methods of an instance.
- Discuss nested classes and Interfaces.
- Write your own complete Java classes.
- A class's variables are called fields (or attributes).
- Every Body object has its own specific instances of these fields, except for nextID.
- Changing the orbits of one object will not affect the orbits of others.
- We declared many fields of Body to be public; this is not always a good design idea.
- Four possible access control modifiers:
- private - members declared private are accessible only in the class itself.
- protected - accessible in the class itself, and are accessible to, and inheritable by, code in the same package, and code in subclasses.
- public - accessible anywhere the class is accessible, and inherited by all subclasses.
- package - members declared with no access modifier are accessible in the class itself and are accessible to, and inheritable by, code in the same package.
- All parameter methods are "pass by value".
- Values of a parameter are copies of the values in the invoking method.
- When the parameter is an object, the object reference is passed by value.
- End result: primitives cannot be modified in methods, objects can.
- If data fields (attributes) are public, programmers can change them.
- Generally want to hide the data from programmers that will use the class.
- If programmers can access a class's fields directly, you have no control over what values they can assign.
- In the Body example, nextIDshould be private.
- If necessary, you should provide get methods that allow programmers to determine the current field value, and set methods to modify the value. These are called accessors (get) and mutators (set).
- You override the signature of a method from a superclass.
- Overriding methods must have argument lists with the identical type and order as the superclass.
- When you override a method of a superclass you should honor the intended behavior of the method.
- Java performs garbage collection for you and eliminates the need to free objects explicitly.
- This eliminates a common cause of errors in C/C++ and other languages (memory leaks). Never have to worry about dangling references.
- When an object is no longer reachable the space it occupies can be reclaimed.
- Space is reclaimed at the garbage collector's discretion.
- Creating and collecting large numbers of objects can interfere with time-critical applications.
- A class can implement a finalize method.
- This method will be executed before an object's space is reclaimed.
- Gives you a chance to use the state of the object to reclaim other non-Java resources.
- finalize is declared like this:
protected void finalize() throws Throwable {
// ...
}
- Important when dealing with non-Java resources, such as open files.
- Example: a class that opens a file should provide a close() method. Even then, there is no guarantee that the programmer will call the close() method, so it should be done in a finalize method.
public void close()
{
if (file != null)
{
file.close();
file = null;
}
}
protected void finalize() throws Throwable
{
try
{
close();
}
finally
{
super.finalize();
}
}
- The close method is written carefully in case it is called more than once.
- super.finalize is called to make sure your superclass is also finalized.
- Train yourself to always do this.
- If an object supports a public toString method that takes no parameters and returns a String object, that method is invoked whenever a + or += expression has an object of that type where a String object is expected.
- All primitive types are implicitly converted to String objects when used in String expressions.
- Used when you need to manipulate some hardware directly, or execute code not written in Java.
- Portability and safety of the code are lost.
- Implemented using an API provided by the people who wrote the virtual machine on the platform where the code will run.
- The standard API for C programmers is called Java Native Interface (JNI).
- Other API's are being defined for other languages.
|
|