A package is a grouping of classes that share a common purpose or category. One member of a package has special privileges to access data and methods in other members of the package, hence the protected access modifier.A package is the Java equivalent of a library. It is a collection of classes which can be made available using the import statement. The following Java statement imports the utility library from the Java API:
Packages are typically held in JAR files, which have the extension .jar or .zip.
Public versus private
An access modifier determines the visibility (essentially the public, private, or protected keyword used in front of any declaration) of a field, method or class to other Java objects.
•A public class, method, or field is visible everywhere.
•A private class, method, or field is visible only in methods defined within that class.
•A protected method or field is visible to methods defined within that class, within sublclasses of the class, or within other classes in the same package.
•The default visibility, known as package, means that the method or field is visible within the class and to other classes in the same package.
A constructor is a special method of a Java class that is called when an instance of the class is created.
Classes can define their own constructors, including multiple, overriding constructors. Which arguments were used in the attempt to create the object determine which constructor is used. When the type, number, and order of arguments used to create an instance of the class match one of the class's constructors, that constructor is used when creating the object.
Garbage collection automatically removes any object with no references to it, with the exception of objects stored as values in a table.
There is no such thing as a destructor method in Java (as there is in C++). Java classes can define their own finalize method for clean up operations when an object is discarded during garbage collection.
Java classes can inherit only from one class. Java uses interfaces instead of multiple-inheritance. A class can implement multiple interfaces. Each interface defines a set of methods and method profiles that must be implemented by the class for the class to be compiled.
An interface defines what methods and static fields the class must declare. The implementation of the methods and fields declared in an interface is located within the class that uses the interface: the interface defines what the class must declare; it is up to the class to determine how it is implemented.
Java error handling
Java error handling code is separate from the code for normal processing.
Errors generate an exception object representing the error. This is called throwing an exception. A thrown exception terminates a Java program unless it is caught and handled properly at some level of the application.
Both Java API classes and custom-created classes can throw exceptions. In fact, users can create their own exception classes which throw their own custom-created classes.
If there is no exception handler in the body of the method where the exception occurred, then the search for an exception handler continues up the call stack. If the top of the call stack is reached and no exception handler has been found, the default exception handler of the Java interpreter running the application is called and the program terminates.
In Adaptive Server Anywhere, if a SQL statement calls a Java method, and an unhandled exception is thrown, a SQL error is generated.
Error types in Java
All errors in Java come from two types of error classes: Exception and Error. Usually, Exception-based errors are handled by error handling code in your method body. Error type errors are specifically for internal errors and resource exhaustion errors inside the Java run-time system.
Exception class errors are thrown and caught. Exception handling code is characterized by try, catch, and finally code blocks.
A try block executes code that may generate an error. A catch block is code that executes if the execution of a try block generates (or throws) an error.
A finally block defines a block of code that executes regardless of whether an error was generated and caught and is typically used for cleanup operations. It is used for code that, under no circumstances, can be omitted.
There are two types of exception class errors: those that are runtime exceptions and those that are not runtime exceptions.
Errors generated by the runtime system are known as implicit exceptions, in that they do not have to be explicitly handled as part of every class or method declaration.
For example, an array out of bounds exception can occur whenever an array is used, but the error does not have to be part of the declaration of the class or method that uses the array.
All other exceptions are explicit. If the method being invoked can throw an error, it must be explicitly caught by the class using the exception-throwing method, or this class must explicitly throw the error itself by identifying the exception it may generate in its class declaration. Essentially, explicit exceptions must be dealt with explicitly. A method must declare all the explicit errors it throws, or catch all the explicit errors that may potentially be thrown.
Non-runtime exceptions are checked at compile time. Java catches many such errors during compilation, before running the code.
Every Java method is given an alternative path of execution so that all Java methods complete, even if they are unable to complete normally. If the type of error thrown is not caught, it's passed to the next code block or method in the stack.