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.

13 comments:

  1. The java launcher is looking for the main method with signature like this:

    public static void main(java.lang.String[] args)

    but your method is:

    public static void main(com.twisters.String[] args)

    It is as simple as that

    ReplyDelete
  2. change String to java.lang.String in your main method.

    When you compile it, the compile references com.twister.String, as it doesn't know main is the entry method.

    However, when running the program, java knows it should look for a static main method with java.lang.String array parameter.

    ReplyDelete
  3. Two classes with the same name can exist provided that they are in two different packages. So there is not compiler error.

    On run time what JVM searches is a main method with a java.lang.String array as a parameter. But on the above case the array is com.twisters.String. So the JVM complains that there is no main method.

    ReplyDelete
  4. Your main declaration picks up the wrong String class. You have fully qualify it using java.lang.String instead.

    twitter.com/lokedhs

    ReplyDelete

  5. Hi
    The problem portrayed above is solved if we have a clear understanding of the Java Namespaces.
    When it is compiled as it is, compiler makes a class file for the String as well as the Demo class. Now, the main takes String as the argument. When we run it, the runtime system starts from the
    public static void main(String[] args) //here, //String is now ur class String
    and therefore it does not match
    public static void main(java.lang.String[] args)
    The solution is to give the fully qualified name for the String class. ie

    package com.twisters;
    class String{}

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

    ReplyDelete
  6. Isn't line 6, "String[] args" going to use the class "String" which you just defined? (thus the main not getting invoked.)

    try changing line 6 to:
    "public static void main(java.lang.String[] args) {"

    ReplyDelete
  7. Replace void main(String[] args
    with void main(java.lang.String...

    ReplyDelete
  8. Declare main as:

    public static void main(java.lang.String[] args)...

    ReplyDelete
  9. your main() method is referencing local String class. Use main(java.lang.String[] args) and it should work.

    ReplyDelete
  10. To run this, you should specify which String class you use in method "main". Should use java.lang.String. Here is the code:

    1 package com.twisters;
    2
    3 class String{}
    4
    5 public class Demo {
    6 public static void main(java.lang.String[] args) {
    7 System.out.println("Challenge Success!!!");
    8 }
    9 }

    ReplyDelete
  11. If you override the String class then Demo would have an overloaded Main method, which would not be recognized on run.

    ReplyDelete
  12. The String class in the main method's parameter isn't java.lang.String, it's the String defined in this program's package, and so that's why when running, it can't find the main method that matches

    main(java.lang.String[]):void

    ReplyDelete
  13. Thank you. Found it interesting and useful. Java is a general purpose, high-level, class-based and object-oriented programming language. And we provide the best Java training in Chennai at Fita.

    ReplyDelete

Solution for this question?