Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why we can not create a class with name String in java?

Posted on 2006-11-21
7
Medium Priority
?
373 Views
Last Modified: 2010-03-31
I am creating one class with name as String.
class String
{
      public static void main(String[] args)
      {
            System.out.println("Hello World!");
      }
}
When i am compiling that is giving the following error.
Exception in thread "main" java.lang.NoSuchMethodError: main

Can you provide me the cause why this error is coming for String only?

But if we are giving the class name Integer,Float, etc any other object it is compiling and running fine.
0
Comment
Question by:smruti_patra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:nishayr
ID: 17993309
Hey there,

I think you can do that, you just need to specify a different package...
i.e.

package myPackage;

class String
{
...
}


and of course, place the String.java file inside the myPackage folder:

myPackage\String.java

Thanks,
Nish
0
 
LVL 3

Accepted Solution

by:
nishayr earned 172 total points
ID: 17993320
Oh, the reason is that the java.lang package is implicitly imported for every java program.

That package contains the String class already (along with all the other basic java types...). So when you declare a String class (or Integer etc.) without specifying a different package, the String classes conflict... and java.lang.String one wins of course... ;)

Thanks,
Nish
0
 
LVL 9

Expert Comment

by:owenli27
ID: 17993349
After you created one class with name as String, I think that your other classes such as Integer, Float cannot run as well.

Since your new defined 'String' class shadow standard String class,  the any classes, which have main(String[] args) method, will automaticlly refer your new "String" class as main method parameters - "String[] args".  
0
 
LVL 9

Assisted Solution

by:owenli27
owenli27 earned 164 total points
ID: 17993384
You will be OK if you change  main(String[] args)
to  main(java.lang.String[] args)
0
 
LVL 92

Assisted Solution

by:objects
objects earned 164 total points
ID: 17993742
or rename your class
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question