• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we create our own String class  RSS feed

 
Nazeer Ahmed
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Is the following code legal

class String{
String(){
System.out.println("String name");
}
}
public class examReview{
public static void main(String args[]){
new String();
}
}

It is in default package.It is compiling fine but when i run it is telling

java.lang.NoSuchMethodError: main
Exception in thread "main"
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess u did a "java String" instead of "java examReview"
 
Nazeer Ahmed
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running it in WSAD.Well if i comment the string class out it is working fine.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing your own String class is certainly not a topic of SCJP. Nor is running it in a WSAD (whatever that is, pardon my ignorance). Moving to Java In General (Intermediate)
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

on performing
> java examReview

the JVM won't find the main method. Because it looks for a main method with the signature
public static void main (java.lang.String [] args)
but it finds only
public static void main (defaultPackage.String [] args)

Therefore the message
java.lang.NoSuchMethodError: main

The code of Nazeer will compile and run if you specify the package:

However, this code will corrupt all other programs in the package that use a String without specifying that it meant java.lang.String


Yours,
Bu.
 
Nazeer Ahmed
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
It is working fine now.

Barry
Actually i got a question in whizlabs in which an user defined class was having the name Short.Thats why i asked this question.Well it was present in a SCJP mock exam simulator(whizlabs SCJP5.0 Exam simulator 6.0.1).I gave the answer as "does not compile" straightway thinking that Short used as class name.So mmmm i think it should be put in JCP forum itself
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problems are twofold:
1) default package. That's a big nono. It might often work, but is very bad practice.
2) your method signature for the main method no longer works to run that class as an application. It REQUIRES the argument to be an array of java.lang.String, due to your String class that's no longer the case.

But yes, you are allowed to define new classes with the name of a standard class.
I think most of us have done so by mistake or otherwise at one point or another and had it come back to bite us some time later.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!