Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

732 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