Solved

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

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

Industry Leaders: 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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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