This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes learning polymorphism and construction Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "learning polymorphism and construction" Watch "learning polymorphism and construction" New topic
Author

learning polymorphism and construction

Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
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?


Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
What did you type when you interpreted it?
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
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

Joined: Feb 07, 2005
Posts: 2341
What I mean is, when you did java ..., what was the exact filename you used?
Jesse Crockett
Ranch Hand

Joined: Feb 03, 2005
Posts: 129
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

Joined: Feb 07, 2005
Posts: 2341
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341

- 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

Joined: Feb 03, 2005
Posts: 129
thanks guys.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
Works for me too...


O/P:

[ March 13, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: learning polymorphism and construction
 
Similar Threads
Programmer's Final Exam question #13 (from imon Robert's book)
Doubt with overloaded super constructor
final variable declaration
Use of constructor in abstract class?
What will be the output of following program? explain why?