I know it's been a long time since my last post, I been very busy and now I just want to talk a little bit about JMX, a topic I came across while doing some light reading; Professional Java 6.
JMX is a technology for managing and monitoring applications systems and network devices. This technology allows you to deploy specialized java classes called MBeans to a software agent and interact with them at runtime.
I hope this sparks a little bit of interest in that developer brain of yours.... any who, I will publish a code example in my next post.
Wednesday, September 30, 2009
Tuesday, July 14, 2009
Phone Book with Jdbc Implementation.
For those of you that are interested in the solution of Saturday's exercise, click here to download the code.
Saturday, July 11, 2009
Friday, July 10, 2009
Saturday, July 4, 2009
Friday, July 3, 2009
Magic Squares
Friday, June 26, 2009
Exceptions
An exception in Java is an object that’s created when an abnormal situation arises in your program. This exception object has fields that store information about the nature of the problem. The exception is said to be thrown—that is, the object identifying the exceptional circumstance is tossed as an argument to a specific piece of program code that has been written specifically to deal with that kind of problem. The code receiving the exception object as a parameter is said to catch it.
Types of Exceptions
An exception is always an object of some subclass of the standard class Throwable. Two direct subclasses of the class Throwable the class Error and the class Exception cover all the standard exceptions. Both these classes themselves have subclasses that identify specific exception conditions.
The exceptions that are defined by the Error class and its subclasses are characterized by the fact that they all represent conditions that you aren’t expected to do anything about, so you aren’t expected to catch them. Error has three direct subclasses—ThreadDeath, LinkageError, and VirtualMachineError.
For almost all the exceptions that are represented by subclasses of the Exception class, you must include code in your programs to deal with them if your code may cause them to be thrown. If a method in your program has the potential to generate an exception of a type that has Exception as a superclass, you must either handle the exception within the method or register that your method may throw such an exception. If you don’t, your program will not compile.
Dealing with Exceptions
Whenever you write code that can throw an exception, you have a choice. You can supply code within the method to deal with any exception that is thrown, or you can essentially ignore it by enabling the method containing the exception throwing code to pass it on to the code that called the method.
To declare that your method can throw exceptions you just put the throws keyword after the parameter list for the method.
If you want to deal with the exceptions where they occur, you can include three kinds of code blocks in a method to handle them try, catch, and finally blocks.
To demonstrate the use of Exceptions and how can you create your own I created a quick example. Click here to download the code.
Types of Exceptions
An exception is always an object of some subclass of the standard class Throwable. Two direct subclasses of the class Throwable the class Error and the class Exception cover all the standard exceptions. Both these classes themselves have subclasses that identify specific exception conditions.
The exceptions that are defined by the Error class and its subclasses are characterized by the fact that they all represent conditions that you aren’t expected to do anything about, so you aren’t expected to catch them. Error has three direct subclasses—ThreadDeath, LinkageError, and VirtualMachineError.
For almost all the exceptions that are represented by subclasses of the Exception class, you must include code in your programs to deal with them if your code may cause them to be thrown. If a method in your program has the potential to generate an exception of a type that has Exception as a superclass, you must either handle the exception within the method or register that your method may throw such an exception. If you don’t, your program will not compile.
Dealing with Exceptions
Whenever you write code that can throw an exception, you have a choice. You can supply code within the method to deal with any exception that is thrown, or you can essentially ignore it by enabling the method containing the exception throwing code to pass it on to the code that called the method.
To declare that your method can throw exceptions you just put the throws keyword after the parameter list for the method.
double myMethod() throws IOException {
// Detail of the method code...
}
If you want to deal with the exceptions where they occur, you can include three kinds of code blocks in a method to handle them try, catch, and finally blocks.
- A try block encloses code that may give rise to one or more exceptions. Code that can throw anexception that you want to catch must be in a try block.
- A catch block encloses code that is intended to handle exceptions of a particular type that maybe thrown in the associated try block. I’ll get to how a catch block is associated with a tryblock in a moment.
- The code in a finally block is always executed before the method ends, regardless of whether any exceptions are thrown in the try block.
try {
// Code that can throw one or more exceptions
} catch(Exception e) {
// Code to handle the exception
} finally {
// Clean-up code to be executed last
}
To demonstrate the use of Exceptions and how can you create your own I created a quick example. Click here to download the code.
Polymorphism
The word polymorphism generally means the ability to assume several different forms or shapes. In programming terms it means the ability of a single variable of a given type to be used to reference objects of different types and to automatically call the method that is specific to the type of object the variable references. This enables a single method call to behave differently, depending on the type of the object to which the call applies.
To demonstrate the use of Interfaces and Polymorphism I created a small example. Click here to download the code.
To demonstrate the use of Interfaces and Polymorphism I created a small example. Click here to download the code.
Interfaces
An interface is essentially a collection of related constants and/or abstract methods, and in most cases it will contain just methods. An interface doesn’t define what a method does. It just defines its form, its name, its parameters, and its return type, so by definition the methods in an interface are abstract.
The Universe Superclass
All the classes that you define are subclasses by default. All your classes have a standard class, Object, as a base, so Object is a superclass of every class. You never need to specify the class Object as a base in the definition of your classes it happens automatically. Your classes will inherit members from the class Object. These all happen to be methods, of which seven are public, and two are protected. The seven public methods are:
The two protected methods that your classes inherit from Object are:
To show you how can you take advantage of the Object class I created a simple example. Click here to download the code.
Method | Purpose |
---|---|
toString() | This method returns a String object that describes the current object. In the inherited version of the method, this will be the name of the class, followed by ‘@’ and the hexadecimal representation for the object. This method is called automatically when you concatenate objects with String variables using +. You can override this method in your classes to return your own String object for your class. |
equals() | This compares the reference to the object passed as an argument with the reference to the current object and returns true if they are equal. Thus true is returned if the current object and the argument are the same object (not just equal—they must be one and the same object). It returns false if they are different objects, even if the objects have identical values for their data members. |
getClass() | This method returns an object of type Class that identifies the class of the current object. |
hashCode() | This method calculates a hashcode value for an object and returns it as type int. Hashcode values are used in classes defined in the package java.util for storing objects in hash tables. |
notify() | This is used to wake up a thread associated with the current object. |
notifyAll() | This is used to wake up all threads associated with the current object. |
wait() | This method causes a thread to wait for a change in the current object. |
The two protected methods that your classes inherit from Object are:
Method | Purpose |
---|---|
clone() | This will create an object that is a copy of the current object regardless of type. This can be of any type, as an Object variable can refer to an objectof any class. Note that this does not work with all class objects and does not always do precisely what you want. |
finalize() | This is the method that is called to clean up when an object is destroyed. |
To show you how can you take advantage of the Object class I created a simple example. Click here to download the code.
Abstract Classes
An abstract class is a class in which one or more methods are declared, but not defined. The bodies of these methods are omitted, because implementing the methods does not make sense. Since they have no definition and cannot be executed, they are called abstract methods. The declaration for an abstract method ends with a semicolon and you specify the method with the keyword abstract to identify it as such. To declare that a class is abstract you just use the keyword abstract in front of the class keyword in the first line of the class definition.
To demonstrate the use of abstract classes I modified the last example. Click here to download the code.
To demonstrate the use of abstract classes I modified the last example. Click here to download the code.
Class Inheritance
Defining a new class based on an existing class is called derivation. The new class, or derived class, is referred to as a direct subclass of the class from which it is derived. The original class is called a base class because it forms the base for the definition of the derived class. The original class is also referred to as a superclass of the derived class.
The inclusion of members of a base class in a derived class so that they are accessible in that derived class is called class inheritance. An inherited member of a base class is one that is accessible within the derived class. If a base class member is not accessible in a derived class, then it is not an inherited member of the derived class, but base class members that are not inherited still form part of a derived class object.
To demonstrate the use of class inheritance I created a quick example, click here to download the code.
The inclusion of members of a base class in a derived class so that they are accessible in that derived class is called class inheritance. An inherited member of a base class is one that is accessible within the derived class. If a base class member is not accessible in a derived class, then it is not an inherited member of the derived class, but base class members that are not inherited still form part of a derived class object.
To demonstrate the use of class inheritance I created a quick example, click here to download the code.
The finalize() method
You have the option of including a method finalize() in a class definition. This method is called automatically by Java before an object is finally destroyed and the space it occupies in memory is released. In practice this may be some time after the object is inaccessible in your program. When an object goes out of scope, it is dead as far as your program is concerned, but the Java Virtual Machine may not get around to disposing of the remains until later. When it does, it calls the finalize() method for the object.
I modified the classes from "our first project" to demonstrate it. Click here to download the updated classes.
protected void finalize() {
// Your clean-up code...
}
I modified the classes from "our first project" to demonstrate it. Click here to download the updated classes.
Nested Classes
When you define a nested class, it is a member of the enclosing class in much the same way as the other class members. A nested class can have an access attribute just like other class members, and the accessibility from outside the enclosing class is determined by the attributes in the same way:
Static Nested Classes
To make objects of a nested class type independent of objects of the enclosing class type, you can declare the nested class as static. A static nested class can have static members, whereas a non-static nested class cannot.
To demonstrate the use of a nested class I created a partial implementation of a LinkedList, click here to download the code.
public class Outside {
// Nested class
public class Inside {
// Details of Inside class...
}
// More members of Outside class...
}
Static Nested Classes
To make objects of a nested class type independent of objects of the enclosing class type, you can declare the nested class as static. A static nested class can have static members, whereas a non-static nested class cannot.
To demonstrate the use of a nested class I created a partial implementation of a LinkedList, click here to download the code.
Encapsulation
Encapsulation refers to the hiding of items of data and methods within an object. This is achieved by specifying them as private in the definition of the class. Being able to encapsulate members of a class in this way is important for the security and integrity of class objects. You may have a class with data members that can take on only particular values. By hiding the data members and forcing the use of a method to set or change the values, you can ensure that only legal values are set.
Another major advantage of encapsulation is the ability to hide the implementation of a class. By allowing only limited access to the members of a class, you have the freedom to change the internals of the class without necessitating changes to programs that use the class. Click here to download the code that demonstrate encapsulation.
Another major advantage of encapsulation is the ability to hide the implementation of a class. By allowing only limited access to the members of a class, you have the freedom to change the internals of the class without necessitating changes to programs that use the class. Click here to download the code that demonstrate encapsulation.
Controlling Access to Class Members
The options you have for specifying the accessibility of a variable or a method in a class are:
This may sound more complicated than it actually is. The following image shows the access allowed between classes within the same package.

The next image shows the access allowed between classes in different packages.
Attribute | Permitted Access |
---|---|
No access attribute | From methods in any class in the same package |
public | From methods in any class anywhere as long as the class has been declared as public |
private | Accessible only from methods inside the class. No access from outside the class at all. |
protected | From methods in any class in the same package and from any subclass anywhere. |

The next image shows the access allowed between classes in different packages.

The Variable this
Every instance method has a variable with the name this that refers to the current object for which the method is being called. The compiler uses this implicitly when your method refers to an instance variable of the class. You could put it in yourself, but there’s no need, the compiler does it for you. In fact, it is not good practice to clutter up your code with this unnecessarily.
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. The names of variables that are declared within a method are local to the method. You can use a name for a local variable or a parameter in a method that is the same as that of a instance variable. If you find it necessary or convenient to do this, then you must use the name this when you refer to the data member of the class from within the method. The variable name by itself will always refer to the variable that is local to the method, not the instance variable. For example, suppose you wanted to add a method to change the radius of a Sphere object to a new radius value that is passed as an argument. You could code this as:
No confusion in the duplication of names exists here. It is clear that you are receiving a radius value as a parameter with the name radius and storing it in the radius variable for the class object.
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. The names of variables that are declared within a method are local to the method. You can use a name for a local variable or a parameter in a method that is the same as that of a instance variable. If you find it necessary or convenient to do this, then you must use the name this when you refer to the data member of the class from within the method. The variable name by itself will always refer to the variable that is local to the method, not the instance variable. For example, suppose you wanted to add a method to change the radius of a Sphere object to a new radius value that is passed as an argument. You could code this as:
void changeRadius(double radius) {
// Change the instance variable to the argument value
this.radius = radius;
}
No confusion in the duplication of names exists here. It is clear that you are receiving a radius value as a parameter with the name radius and storing it in the radius variable for the class object.
Method Overloading
Defining two or more methods with the same name in a class is called method overloading. The name of a method together with the types and sequence of the parameters form the signature of the method, the signature of each method in a class must be distinct to allow the compiler to determine exactly which method you are calling at any particular point. The return type has no effect on the signature of a method. You cannot differentiate between two methods just by the return type. This is because the return type is not necessarily apparent when you call a method. You can see an example in the following code :
Constructors are methods that can be overloaded, just like any other method in a class. In most situations, you will want to generate objects of a class from different sets of initial defining data, I modified the classes from "our first project" to demonstrate it. Click here to download the updated classes.
class TryMethodOverloading {
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
public static String valueOf(int i) {
return Integer.toString(i, 10);
}
}
Constructors are methods that can be overloaded, just like any other method in a class. In most situations, you will want to generate objects of a class from different sets of initial defining data, I modified the classes from "our first project" to demonstrate it. Click here to download the updated classes.
Thursday, June 25, 2009
Our first project
Is time for our first project, in which we are going to see how to define a Class, methods, how to create Objects and how constructors work. Click here to download the source code.
More OOP concepts
Members Of a Class
In essence, a class definition is very simple. There are just two kinds of things that you can include in a class definition:
Analogous to the variables in a class definition are two varieties of methods instance methods and class methods. You can execute class methods even when no objects of a class exist, whereas instance methods can be executed only in relation to a particular object, so if no objects exist, you have no way to execute any of the instance methods defined in the class. Again, like class variables, class methods are declared using the keyword static, so they are sometimes referred to as static methods.
Constructors
A constructor is a special kind of method that is always invoked when you create an object. A constructor has two special characteristics that differentiate it from other class methods:
In essence, a class definition is very simple. There are just two kinds of things that you can include in a class definition:
- Fields : These are variables that store data items that typically differentiate one object of the class from another. They are also referred to as data members of a class.
- Methods : These define the operations you can perform for the class so they determine what you can do to, or with, objects of the class. Methods typically operate on the fields the variables of the class.
- Non-static fields, also called instance variables : Each object of the class will have its own copy of each of the non-static fields or instance variables that appear in the class definition. Each object will have its own values for each instance variable.
- Static fields, also called class variables : A given class will have only one copy of each of its static fields or class variables, and these will be shared between and among all the objects of the class. Each class variable exists even if no objects of the class have been created.
Analogous to the variables in a class definition are two varieties of methods instance methods and class methods. You can execute class methods even when no objects of a class exist, whereas instance methods can be executed only in relation to a particular object, so if no objects exist, you have no way to execute any of the instance methods defined in the class. Again, like class variables, class methods are declared using the keyword static, so they are sometimes referred to as static methods.
Constructors
A constructor is a special kind of method that is always invoked when you create an object. A constructor has two special characteristics that differentiate it from other class methods:
- A constructor never returns a value, and you must not specify a return type—not even of type void.
- A constructor always has the same name as the class.
Tuesday, June 23, 2009
Object Oriented Programming Concepts
What is a Class?
A class is a specification, or blueprint expressed as a piece of program code that defines a particular sort of object.
What is an Object?
An Object is an instance of a class.
A class is a specification, or blueprint expressed as a piece of program code that defines a particular sort of object.
What is an Object?
An Object is an instance of a class.
The "Hello World!" Application
Every Java application contains a class that defines a method called main(). The name of this class is the name that you use as the argument to the Java interpreter when you run the application. You can call the class whatever you want, but the method which is executed first in an application is always called main(). When you run your Java application, the method main() will typically cause methods belonging to other classes to be executed, but the simplest possible Java application program consists of one class containing just the method main(). As you will see below, the main() method has a particular fixed form, and if it is not of the required form, it will not be recognized by the Java interpreter as the method where execution starts.
Type the above code using your favorite plaintext editor in the folder you want with the name HelloWorld.java Bring up a shell, or "command," window. You can do this from the Start menu by choosing Run... and then entering
class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
Type the above code using your favorite plaintext editor in the folder you want with the name HelloWorld.java Bring up a shell, or "command," window. You can do this from the Start menu by choosing Run... and then entering
cmd
. To compile your source file, change your current directory to the directory where your file is located (in my case is c:\) , if you enter dir
at the prompt, you should see your source file.Now you are ready to compile. At the prompt, type the following command and press Enter. The compiler has generated a bytecode file,javac HelloWorldApp.java
HelloWorldApp.class
. At the prompt, type dir
to see the new file that was generated. Now that you have a .class
file, you can run your program. At the prompt, type the following command and press Enterjava HelloWorldAppThe following image depict the above steps.

Wednesday, June 17, 2009
Getting ready
To write your first program you'll need the Java Development Kit (JDK) which can be download it from http://java.sun.com/javase/downloads/index.jsp.
After downloading and installing the JDK you'll need an IDE; I suggest NetBeans which can be download it from http://www.netbeans.org/downloads/index.html. Of course you could use any IDE of your preference but NetBeans will be used to develop the examples in this blog.
After downloading and installing the JDK you'll need an IDE; I suggest NetBeans which can be download it from http://www.netbeans.org/downloads/index.html. Of course you could use any IDE of your preference but NetBeans will be used to develop the examples in this blog.
Why Use Java?
The most important characteristic of Java is that it was designed from the outset to be machine independent. You can run Java programs unchanged on any machine and operating system combination that supports Java. An application written in Java will only require a single set of source code statements, regardless of the number of different computer platforms on which it is run.
Possibly the next most important characteristic of Java is that it is object-oriented. Object-oriented programs are easier to understand and less timeconsuming to maintain and extend than programs that have been written without the benefit of using objects.
Possibly the next most important characteristic of Java is that it is object-oriented. Object-oriented programs are easier to understand and less timeconsuming to maintain and extend than programs that have been written without the benefit of using objects.
Tuesday, June 16, 2009
Welcome to my Java blog
Hi everybody and welcome to my Java blog
The main idea of this blog is to give you a taste of what you can do with Java. In the next days I will be posting a few examples starting with a few basic samples and building on from that.
So keep checking now and then, and happy coding.
The main idea of this blog is to give you a taste of what you can do with Java. In the next days I will be posting a few examples starting with a few basic samples and building on from that.
So keep checking now and then, and happy coding.
Subscribe to:
Posts (Atom)