Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Wednesday, April 15, 2009

Puzzle 15 - Solution

The key to the last puzzle is ‘when using classes with same names, use fully qualified class
names’.

The statement public static void main(String[] args) is legal as far as the compiler is
concerned. It resolves to String to com.twisters.String and is legally used in a method
called main().

However at run time the class loader looks for a main method having java.lang.String[] as
parameter. Since it does not find one it gives an Exception in thread "main"
java.lang.NoSuchMethodError.

The solution to the problem is to use the fully qualified class name while declaring the
main method public static void main(java.lang.String[] args)

Congrats to
1. Eugene Kuleshov
2. Kasunh s
3. Lokedhs
4. TimTowTdi
5. Kai
6. TioDuke
7. Grr
8. James Barrow
for getting the correct answer.

Leader Board will be updated with the next post...

Sunday, April 12, 2009

Puzzle 15 – Package and My String.

Language – Java | Type – Concept | Last date 15-Apr-2009 12:00 p.m. IST | Points 2

Someone told me that I could not write a class called ‘String’ as it is sacred class. Writing a class called ‘String’ would cause the java compiler to complain louder than hell. All rubbish of course!

So to prove Mr. Someone wrong I quickly wiped up a String class in my own package. I also threw in a Demo class – though everything compiles fine, I can’t run Demo anymore. Any help out here?

package com.twisters;

class String{}

public class Demo {
public static void main(String[] args) {
System.out.println(
"Challenge Success!!!");
}
}

Got an answer? Why don’t you leave it here.