Java Reference Index


  1. Introduction
  2. Data Types
  3. Arrays
  4. Operators
  5. Program Control
  6. Classes
  7. Objects
  8. Interfaces
  9. Containers
  10. Class Structure
  11. Error Handling

    Program Types

  12. Applications
  13. Applets

    How To

  14. Type Conversions

    Packages

  15. java.lang.reflect
  16. java.applet
  17. java.awt
  18. java.beans
  19. java.io
  20. java.lang
  21. java.math
  22. java.net
  23. java.rm
  24. java.security
  25. java.sa
  26. java.text
  27. java.util
  28. java.servlet

    Classes

  29. Object Class

    Appendices

  30. Terms
  31. Credits

Classes

Class Creation

The following keywords are used to define characteristics of the class when it is created:

  • abstract - The class cannot be instantiated. It can only be subclassed. This is also used to createe methods that do not have implementations in the base class. Abstract methods may only be declared in a abstract class. The subclass must implement the method unless it is an abstract class also.
  • class - Indicates a class declaration.
  • extends - Used to identify the superclass that this class will be a subclass to.
  • final - The class cannot be used to create subclasses.
  • implements - Used to implement an interface to be used with the class.
  • public - Allows the class to be used by any package. It is by default only available to the package it is contained in.

An example declaration:

public class MyClass extends MainClass
{
   int data1;
   public MyClass()  //Constructor
   {
      super("MyClass");
      data1=0;
   }
   public Method1()
   {
   }
   private Method2()
   {
   }
   protected Method3()
   {
   }
}

The methods and variables may be class methods or variables or instance methods or variables depending on how they are declared. If declared using the keyword "static" they are class variables or methods.

Class Constructor

The system provides the constructor by default if it is not written into your class. Also you can write one or more additional constructors that may take various arguments. If you declare the object using a specific set of arguments, the appropriate constructor will be used to create the class. The line:

super("MyClass");

calls the constructor for the MyClass superclass which is MainClass (since the extends keyword declares it as a subclass to Mainclass). In the example above, the constructor is publically accessible. Constructor access specifiers:

  • package - Only classes in the same package can declare an instance of your class.
  • private - No other class can create an instance of your class
  • protected - Only subclasses can create instances of the class.
  • public - An instance of the class can be made by any class.

A constructor method has the same name as the class and return no type (void). There may be more than one class constructor with different parameter lists and one constructor may call another using the "this" keyword. The constructor for the superclass may be called using the keyword "super". The superclass constructor must be called from the first line in your subclass constructor. It is called by default.

Variable Declaration

Keywords and meanings:

  • Access level - One of the following access levels are declared
    • Public
    • protected
    • package
    • private
  • final - The member value cannot change used for unchanging values.

    final double pie = 3.1428;

  • static - The variable is a class variable and not an instance variable.
  • transient - Used for object serialization
  • volatile - An advanced feature preventing some compiler optimizations on a member

Method Declaration

Keywords:

  • abstract - There is no implementation of this method and it must be a member of an abstract class.
  • Access level - One of the following access levels are declared
    • Public
    • protected
    • package
    • private
  • final - Subclasses cannot over ride this method.
  • native - the method is implemented in a language other than Java.
  • static - This is a class method and cannot be instantiated.
  • synchronized - For threads that access the same data so the data is accessed safely when more than one thread may access it.

An additional keyword is throws which is used to declare an error type that the method may throw if an error occurs.

Objects or primitive data types may be passed to methods. When primitive type arguments are passed to a method, the value of the primitive such as an integer value is passed. An object is not passed. This means that the method the primitive type is passed to cannot change the primitive type's value. This is because it is an immediate value and not an actual variable. This value can only be read. If an object is passed to the method rather than a primitive data type, then any method in the passed object can change an attribute of that object.

Keywords for Reference

  • this - Used to explicitly refer to a member variable or method. If there is a name clash such as if a passed variable has the same name as a member variable, this keyword is necessary. Thie keyword is also used to refer to methods and can be used to call a class constructor.
  • super - Used to refer to member variables or methods of the superclass even if they were overridden in the subclass.

Other Considerations

  • Static initialization blocks - Initializing a class member.
    import java.util.ResourceBundle
    class MyClass
    {
       static ResourceBundle errStr;
       static {
          try {
             errStr = ResourceBundle.getBundle("ErrorStrings");
          } catch (java.util.MissingResourceException e) {
             //  error code
          }
       }
    }
    
  • finalize method - Used to override the superclass finalize method for cleanup when the object is no longer required