Archive for the ‘Java Basics’ Category

How coding to Interface and Inheritance helped me reuse code

Before going into the details I will state the requirement:

  • I need to fetch certain records from 3 different tables into 3 ArrayLists.
  • I have 3 Classes which store the information: Lets name it- Class1, Class2, Class3.
  • All the above 3 classes implement the Interface, lets name it Interface1. The interface has a method called init(). The main task of init() is to initiate the values of the instance variables with the values obtained from the ResultSet (database). This instance is then added to one of the 3 lists define above depending on the type of the reference.

Read more…

Categories: Java Basics Tags: ,

Working with Java Enumerated types (Enums)

December 1, 2009 10 comments

In this post I would like to explain about Enums in Java. Though in my 2 years of coding in Java I have seldom used Enums but they do provide a lot of features when we are required to create a say limited instances of certain type. In this post I have made use of an example written out of inspiration from the example program given in “A Programmer’s Guide to Java SCJP certification” by Khalid A. Mughal and Rolf W. Rasmussen. The example is given in the Section Enumerated Types. I will be developing the Enum as we go through the article.

What is an Enumerated Type or Enum tpye? Enum Type defines a finite set of symbolic names and their values. Suppose we have to define 3 constants- LOW, MEDIUM and HIGH to denote the 3 speeds of Rotation. We can do it by declaring static final variables as follows-

class Speed{
   public static final int LOW=1;
   public static final int MEDIUM=2;
   public static final int HIGH=3;

Read more…

Monitoring and Profiling using VisualVM-1

I executed a simple GUI application which would load the CSV file and parse it and show the contents in a JTable. When the applications started- There was a JFrame, 2 JPanels, a JLabel and a JButton with an Icon. I wanted to monitor the Heap size variations, the number of Classes, Threads details and also wanted to profile the application. So i thought of using VisualVM. The following are the results and snapshots of profiling using VisualVM. Note that the application had only one public class MainFrame in gui package. Also note that i was using the Nimbus Look and Feel.

Read more…

First Attempt @ Servlets, JSP and Cookies

I have been doing Servlets and JSPs from quite some days and yeah they have been lots of interesting stuff in it, And this is my first attempt at Web Technologies. I would recommend beginners to use Head First Servlets and JSP book. It’s a superb book to read, one can enjoy reading and the concepts are taught in terms of process- like how they happen. Some time back i had recommended Head First Java. One can read the review here.

As it is the first time i am doing some thing in Servlets and Cookies, the below given example is a pretty simple one which explains the use of cookies in communication between client and server lasting more than one request.

Read more…

Categories: Java Basics

Passing Variables into Methods- Java Passing Mechanism Explained

Every Programming Language has its own way of passing the variables into methods. There are basically two ways of passing variables- Pass by Reference and Pass By Value. The former deals with the passing of reference or pointer to particular variable and the latter involves passing a copy of the variable to the method. C supports both ways of passing and the Pass By Reference is supported b means of Pointers. Every Java Beginner ponders over “Is Java a Pass By Value?” or “Is it a Pass By reference?”  or “Both?”. Before answering the question i would like to throw light on Passing Object Reference variables and Passing primitive reference variables.

Object Reference Variables: These are the variables which refer to the object of the declared type or its subtype. Something like

Animal a = new Animal()

where  a is the reference variable

Passing Object Reference Variables:

When a object variable is passed into the method, only the copy of the Object reference is passed and not the Object itself. The bit pattern in the reference variable is copied into the method parameter. What is this bit pattern? This bit pattern is the address of the specific object in the memory (on the heap). In other words, both the caller and the called method will now have identical copies of the reference and thus both will refer to the same exact object on the heap.

Read more…

Initialization Blocks in Java

Apart from methods and constructors, Initialization Blocks are the third place in a Java Program where operations can be performed. Initialization Blocks come in two flavours:

  • Static Initialization Blocks: Runs first when the class is first loaded. Declared by using the keyword “Static”
  • Instance Initialization Blocks: Runs every time when the instance of the class is created.

Read more…

Shadowing Variables in Java Demystified

One of the meanings of the word “Shadow” in the Oxford Dictionary is “a weak or less good version”. Shadowing in Java is also something similar. One can shadow a variable in several ways. I would try to describe the one most comman ways which would trip most of us i.e. “Hiding an instance variable by shadowing it with a local Variable”. What exactly is Shadowing in Java? Shadowing is nothing but redeclaring a variable that’s already been declared somewhere else. The effect of Shadowing is to hide the previously declared variable in such a way that it may look as though you’re using the hidden variable, but you’re actually using the shadowing variable (The Code Snippet below will make it more clear). This most of the times happens by accident and causes hard-to-find bugs.

Read more…

Categories: Java Basics Tags: , , ,