Solved

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

Posted on 2006-11-21
7
365 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 43 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 41 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 41 total points
ID: 17993742
or rename your class
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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