Solved

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

Posted on 2006-11-21
7
321 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now