Java Reference
In-Depth Information
These libraries are built using classes. Classes are then combined under packages. All these
packages are in turn kept under a library. Classes help greatly in building these libraries.
Accessing the code inside these libraries is also easy. The programmer just has to include
these libraries inside their code. When the source code of their software product is run, the
code inside the libraries also runs.
Imagine how difficult it will be if all the libraries provided in the programming language
were just methods. Managing and then accessing them will be extremely difficult.
For the same reason, building software products using classes make your source code man-
ageable and accessible. You can create packages (explained in the next section) which will
include your classes. Classes off course include their own methods and data. For example
if you have created a package named “my_package” and have included a class named
“my_class” then you can call your class as “my_package.my_class” anywhere in your
source code. If you have included a method named “my_method()” in this class then you
can call this method using a statement “my_package.my_class.my_method()” anywhere in
your source code.
The other advantage of classes is that they act as template for creating objects. When you
run your source code; objects are created which hold their own data. The big thing about
objects is that they are created when they are needed and destroyed when they are not
needed. This helps in memory management. When an object is destroyed, the memory oc-
cupied by the object is freed and can be used by new objects.
When we write source code, we write classes. But when we think of programming tasks to
be done we should think in terms of objects.
One more point to think why class concept was conceived is to think about accessibility
of variables and methods. In procedural languages, methods are the top level elements in
programming. Naturally they should always be declared public. If they are declared private
then they can not ever be called anywhere in your program and result in dead code (code
which can never execute). In many instances, private methods are required. Generally in
procedural programming languages this concept is difficult to implement. To circumvent
this problem, procedural programming languages devised some structures which somehow
made some methods private. For example in VB.Net they created a structure known as
module. Inside these modules you can keep private methods. These methods were access-
ible only inside the module and could not be called from outside. Class concept has made
Search WWH ::




Custom Search