Sunday, December 18, 2011

Static Block of a Class

This post intended to be a quite quick. So let's begin. Could you tell me the output of the code written below?


public class EmptyClass {

static{
System.out.println("Hello World.");
}

}

There can be following options
A. It will not compile.
B. It will compile but it will not run and it will just print exception.
C. It will compile and it will not run but it will print "Hello World" and then will print the exception.
D. It will compile and Run without issues.

After seeing the choice, answer looks quite simple. Is not it?
Anyways lets analyze.
A. It will compile as static blocks are allowed in java and java does not force you to write main method in each and every class. So there are not any issues while compiling the class. So A is not the right option.

B. It will not run, true. As java says, there should be main method to run an application. There is not any run method is the application. But there is a static block in the application. What will happen to this static block? This options seems to correct but puzzle has not completely solved yet. Lets move forward and see if B is correct or there is any other better option available.

C. We accept it will compile but it will not run as main method is not available in the class or JVM. But as java say, before as soon as it loads any class in memory it executes all its static blocks first. So to run the application class will be loaded into the memory so static block exists in the class will executed which in turn will print "Hello World". So it seems to be most appropriate answer.

D. Do we have any scope and requirement left to discuss last options. No

I learnt this lesson while discussing few basic java questions with my friend. Nice but tricky is not it... Anyways will stop here. B bye.

Tuesday, December 13, 2011

Problem with Class Downcast

Sometime I think, I have gained a lot of knowledge in Java and I know lot of things in Java, but once in a while Java keeps pushing my believes to the corner and demonstrates it's superiority. Most of times it is basic java which deceives me. Here is my story.

Today one of my colleague asked one question and I did not have any answer for his question. The issue in concern belongs to the program written below.


class SuperClass {

void display(){
System.out.println("SuperClass Display.");
}

}

class SubClass extends SuperClass{

void display(){
System.out.println("SubClass Display.");
}


public static void main(String[] args){
SuperClass superClsInstance = new SuperClass();
SubClass subClsInstance = (SubClass) superClsInstance;
subClsInstance.display();
}

}

In this case I assumed downcast should happen without any issues and it should print message based on object instance. So in this case subClsInstance is referring to Object of SuperClass and it should print message mentioned below.
SuperClass Display

But no. It is not so simple.

It will throw ClassCastException. Heck Why? What have I done which made it throw such stupid and merciless exception? I am devastated and I do not even know basic of DownCasting, shame on me a 5 year experience java developer do not know such small thing. Anyways let me find out the reason for this behavior.

Hmmm, Interesting na.... I always knew that I am not very good in java and google and Sun have proved me right. As per the information available downcast is possible if and only if object created is a type of Class reference being downcasted to..

Lets take few example... Dog is an animal and An animal is dog only if it is a Dog. If you write same thing in the code so it would be...

Animal ani = new Dog();
Dog dg = (Dog) ani;

So in example listed above, Dog is an animal and it will down cast without problems as code is saying Dog is Dog...


Animal ani = new Animal();
Dog dg = (Dog) ani;
So here in example listed above, here Animal is an animal and it will not down cast as code is saying Animal is Dog...It can not be, we do not have information about Animal. This can be some other animal so Java can not assume anything. It will say that it does not have any idea about this animal so Buzz off.

In our case same thing is happening.We have created an object of SuperClass and asking it to tie up with SubClass. Java says no, do not bog me out with such stupid things and take care of your code better.

Oops java is right and I am wrong. Cool then I learnt something today and shared with you all. Hope you will learn and will not cause such stupid problems in code and will not screw up your interview. 


PS: Stay away from downcasting it is not only but also venomous and code safe.