Results 1 to 3 of 3
Thread: A query about main method
- 12-23-2007, 08:59 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 70
- Rep Power
- 0
A query about main method
I understand that main method is the point, from where the program execution starts. Makes a lot of sense. If I have a package, can that package have 2 classes with main methods defined? If yes, from where the execution will start as there are 2 execution points now. Please briefly explain.
- 12-23-2007, 07:30 PM #2
If I have a package, can that package have 2 classes with main methods defined?
Yes. You can put/have a main method in any class. This is a way to allow testing of a class.
If yes, from where the execution will start as there are 2 execution points now.
The jvm looks for a class in a file with the public modifier and a main method for/at startup. The name of the source file, the (exact same) name of the class in the file that has the public modifier and that has a main method is the starting point. This classes main method will be the starting point.
In packages you usually have each class in a separate file. If a file has multiple outer classes then you may have only one outer class with the public modifier in a file.
In java, let's say I have a class "SkyScraper" and run it with "java ScyScraper" at the prompt. Then the jvm looks in the SkyScraper.class file for
Java Code:public class SkyScraper { ... /** The jvm looks for this as the starting point. */ public void main(String[] args) { ... } } class WindowWasher { }
- 12-24-2007, 10:44 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
Calling main method
By eva in forum New To JavaReplies: 7Last Post: 11-06-2009, 02:37 PM -
Main method with throws Exception
By bugger in forum New To JavaReplies: 3Last Post: 01-07-2008, 03:48 PM -
Private main method
By bugger in forum New To JavaReplies: 1Last Post: 12-21-2007, 10:45 AM -
main method
By eva in forum New To JavaReplies: 5Last Post: 12-19-2007, 10:25 AM -
The main method in java...
By lenny in forum New To JavaReplies: 1Last Post: 07-31-2007, 07:21 AM
Bookmarks