• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

learning polymorphism and construction

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of just doing exercises from the textbook, I've decided to write some of my own classes from scratch to get a better feel for how everything is working. I wrote the following code, which compiles but gives a runtime exception, "no such method: main." I'm trying not to use any superfluous stuff, hoping it will lead to a more solid understanding of what's going on. So, plz tell me, what's going on?


 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you type when you interpreted it?
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you rephrase the question? I don't understand what you are asking.
[ March 13, 2006: Message edited by: Jesse Crockett ]
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is, when you did java ..., what was the exact filename you used?
 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed PolyTest3 and its constructors to public, then renamed the file PolyTest3.java and it works now. Thanks.

Tho it's still unclear what I should learn from this...
[ March 13, 2006: Message edited by: Jesse Crockett ]
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is that you're allowed only one top-level public class in a source file, and the name of the source file must match the name of the public class. Remember Java is case-sensitive.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jesse Crockett:
I changed PolyTest3 and its constructors to public, then renamed the file PolyTest3.java and it works now. Thanks.

Tho it's still unclear what I should learn from this...



- with the class not being public, the main method wasn't accessible to the JVM. That is, a main method not only needs to be public, it also needs to be in a public class,

- a public class needs to be in a file with the same name, and

- making the constructor public wasn't necessary, I think. It should still work if you change that back.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


- with the class not being public, the main method wasn't accessible to the JVM. That is, a main method not only needs to be public, it also needs to be in a public class,



I'm not sure about this statement. I have experimented with the following source file, and it compiles and runs correctly even though the class itself is not declared public. I do agree that the main method needs to be public.

 
Jesse Crockett
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
I'm not sure about this statement. I have experimented with the following source file, and it compiles and runs correctly even though the class itself is not declared public. I do agree that the main method needs to be public.



Frankly, I'm not fully sure either.

What JDK did you try this with? As far as I know, JVMs before Java 5 had some bugs regarding this - in Java 1.2, the main method even didn't have to be public, if I remember correctly.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works for me too...


O/P:

[ March 13, 2006: Message edited by: Garrett Rowe ]
 
grapes are vegan food pellets. Eat this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!