Friday, June 30, 2006

Loading a resource with ClassLoader and ServletContext

Friday, June 23, 2006

Invoking and manipulating static variables from different classes

class AQ {
static int t = 6;
int g = 10;
}

class Tr {

public static void main(String[] args) {

//AQ q = new AQ(); // will also work.
AQ q = null;
System.out.println(q.t);

q.t = 5;

System.out.println(q.t);
new Tr().call();
}

AQ a; // ( by default reference variable "a" is assigned null, since you are declaring it at instance(global) level )

void call() {
System.out.println(a.t);
//System.out.println(new AQ().t);

/*
Both the SOP's prints the same value(5) for variable "t" of class - AQ.
*/
}
}

Output:

6
5
5

class Ts {

public static void main(String[] args) {

//AQ q = new AQ(); // will also work.
AQ q = null;
System.out.println(q.t);
}
}

Output:

6

My question is in the call() method of class - Tr output for static variable of class AQ is 5 whereas in class - Ts output for the same static variable of class AQ is 6.


You started the JVM with the Java command( java ) twice.

There is only one instance per JVM, hence the second time you started the JVM, variable "t" will have its original value(6) that has been assigned to it in its own class AQ.

So when you first executed class - Tr and modified the static variable "t" of class - AQ with its reference variable "q" and assigned a new value, all these happened with in the same JVM and single instance of class - AQ. Thats why you got the modified value(5) being printed out in the method - call().

Whereas with class - Ts, you started the JVM seperately using the "java " command.

One more important point to note here is we are using the reference variable "q" to invoke the static variable of class AQ and the reference variable is assigned with "null". Did you ever think why we didn't end up with a NullPointerException. Thats because we are invoking the static variable and NOT instance variable.

instance variable - belongs to the object and
static variable - belongs to the class.

If you invoke a instance variable using a reference variable thats assigned with "null" then you will get
java.lang.NullPointerException.

Exception Handling Logic

Is it good practice to include all non-exception causing code inside a try block?

If you have a sequence of operations which should be aborted if an exception occurs, then that's exactly what a try block is for. And as a general principle, try blocks should be as large as possible, for this makes the flow of the normal code clearer, and concentrates error handling in one place.

Wednesday, June 14, 2006

Encapsulation and Abstraction

Definition and Difference

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.

Encapsulation - Information hiding

Abstraction is a more generic term which can be considered as an layer on top of encapsulation.

Inheritance is the process by which one object acquires the properties of another object.

Polymorphism is the ability of the super class reference to refer to its own objects as well as objects of its sub-class at runtime.

The ability to invoke overriden methods of subclass based on the object type using super-class reference at runtime is known as polymorphism

Tuesday, June 13, 2006

JAVA_HOME

JAVA_HOME - Why we need to set this variable? (I hope this is an environment variable just like path and classpath).

Yes, it is an environment variable just like PATH and CLASSPATH.

You don't need to set it to be able to use the JDK.

Some programs (for example Ant and Tomcat) find the JDK by looking at the environment variable JAVA_HOME, so you might need to set it if you want to use those programs.

Do all these environment variables like path, classpath,java_home has to be set using capital letters. If so why?

convention.

Some operating systems are case sensitive, and in the past many would use capitals only.

Scalable & Robust - Definition

Scalable - In telecommunications and software engineering, scalability indicates the capability of a system to increase total throughput under an increased load when resources (typically hardware) are added.

Robust - In computing terms, robustness is reliability or being available seven days a week, twenty-four hours a day. Robustness is an important characteristic of the internet because network design is a key factor in the availability of data.

It means the ability to keep running when unexpected events (which can cause Exceptions) occur.

"able to survive partial failure"

I also like to talk about robust design, able to survive fundamental unexpected changes in requirements and technologies.

Thursday, June 01, 2006

Java 5.0 Tutorial

Static variables and instance variables

If static variables belong to the class and NOT part of the object then how come we are able to invoke it with an object reference variable and also modify its value.

class AQ {
static int t = 6;
int g = 10;
}

class Tr {
public static void main(String[] args) {
AQ q = new AQ();
System.out.println(q.t);
q.t = 5;
System.out.println(q.t);
new Tr().call();
}

void call() {
System.out.println(new AQ().t);
}
}


Output:

6
5
5

That is allowed in Java. But be careful.

If you use an object reference to call a static method in a class definition, then it does not matter which object reference you use, even if that reference is null.

Only the type of the reference is considered.

class Ts {
public static void main(String[] args) {
// AQ q = new AQ(); // 1
AQ q = null; // 2
System.out.println(q.t);
}
}

Output:

6

[With 2(q = null) - If you invoke the static variable we will not get any exception where as if we invoke instance variable like q.g we will get java.lang.NullPointerException.]

Here the output is 6 because it's a totally new class called Ts where we are instantiating the class AQ. Hence it reflects the value 6 with which class variable(static) t has been initialized with in class AQ.

Once you initialize a class(static) variable with a new value with in your class using the reference variable of the external class and if you access the same variable with a new instance of the external class still you get to see the same value that has been set with in your class and NOT the value that has been initialized with in the external class.

This is because static variables are accessed based on class(reference) type and NOT Object type.

Static variables are initialized at the class loading time, instance variables are initialized before a constructor is executed.


What is the difference between class variables and instance variables ?

The difference is that no matter what instance of an object you use, you can change the class variables. It doesn't have anything to do with an instance.

However, you can only access an instance variable with a particular instance.

code:

class AQ {
static int t = 6;
int g = 10;
}

class Tr {
public static void main(String[] args) {
AQ q = new AQ();
System.out.println(q.t);
System.out.println(q.g);
q.t = 5;
q.g = 123;
System.out.println(q.t);
System.out.println(q.g);
new Tr().call();
}

void call() {
System.out.println(new AQ().t);
System.out.println(new AQ().g);
}
}


Output:

6
10
5
123
5
10