Results 1 to 10 of 10
Thread: Confusion about main method
- 05-11-2011, 02:40 AM #1
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
Confusion about main method
I have been learning about Java for a few months now and every tutorial or text book tells me that when a program is compiled, that the JDK looks for a main method and starts from there. However, I thought I would test to see what would happen and so I created 2 classes, each containing a main method and then in the first class I created a method outside it's main method. Then in the main method of both classes I created an object from the first class and called the method that I created outside the main method of the first class. I then compiled the second method and it compiled successfully. However, this seems to be a contradiction to what I have been learning about.
Can anyone tell me what is going on?
- 05-11-2011, 02:49 AM #2
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
Further experimenting
Here is the code that I had:
Java Code:public class Test { void printMe(){ System.out.println("Method in Test"); } public static void main(String[] args){ Test tester = new Test(); tester.printMe(); } }I tried something else with this and found something very interesting:Java Code:public class Test2 { public static void main(String[] args){ Test tester = new Test(); tester.printMe(); } }
I called the printMe method twice in the Test class and I found that when I compiled and run that class, that it did print out twice. Then I compiled and run the Test2 class and I found that it only printed once.
Would I be right in saying that when I compile Test2, I am overriding the main method in Test?
- 05-11-2011, 03:09 AM #3
The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance").
Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, drawString, drawInteger, drawFloat, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list.
public class DataArtist {
...
public void draw(String s) {
...
}
public void draw(int i) {
...
}
public void draw(double f) {
...
}
public void draw(int i, double f) {
...
}
}
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
- 05-11-2011, 03:14 AM #4
Taking it backwards...
No, you're not overriding anything, on two accounts: First, a static member (member -> method or field/variable) can't be overridden, only hidden. Second and equally important, overriding applies in cases of inheritance, and your Test2 doesn't extend Text. Even adding an instance method like printMe() in Test2 wouldn't override printMe() in Test as the two classes aren't related by inheritance.
Classes
This is also wrong:
Don't confuse compiling with running/executing. It's the Java runtime -- the JRE, or JVM -- that looks for a method with the correct signature, and uses that as an entry point to the program.when a program is compiled, that the JDK looks for a main method and starts from there.
Execution
dbLast edited by DarrylBurke; 05-11-2011 at 03:18 AM. Reason: Added link
- 05-11-2011, 03:19 AM #5
No, you are not overriding the main method. Overriding is when a child class has a method with the same signature as the parent class. Since your 2 classes do not involve inheritance there is no overriding involved.
You simply have 2 classes that have the same method that does the exact same thing. Each class that is written can have a main method, it just depends upon which one you execute.
To further illustrate or maybe confuse:
You can have as many Test classes as you like, they just all do the same thing: create a Foo object and call the doStuff method.Java Code:class Foo { public void doStuff() { System.out.println("Hello world"); } } class Test1 { public static void main(String[] args) { Foo f = new Foo(); f.doStuff(); } } class Test2 { public static void main(String[] args) { Foo f = new Foo(); f.doStuff(); } } class Test3 { public static void main(String[] args) { Foo f = new Foo(); f.doStuff(); } }
- 05-11-2011, 03:28 AM #6
Just to clarify something, this is not true. The JDK does not look for a main method when you compile a class. A class without a main method will compile perfectly fine (providing all other code is correct).
It is the JVM that looks for a main method when you try to run a program.Last edited by Junky; 05-11-2011 at 03:30 AM. Reason: Ahh crud! Didn't read Daryl's reply.
- 05-11-2011, 03:38 AM #7
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
ok, thanks, and I did have another question which was: If there are many main methods in a Java program, how does the JRE know where to enter? but first I thought I would create a third class without a main method and attempt to create objects of Test and Test2, but it would not compile so I guess it must be whatever main method is in the class I choose to compile that determines the entry point for the JRE.
- 05-11-2011, 03:57 AM #8
You are still confused about compiling and running. Using the code in my above example:
This will successfully compile the Foo class. It has nothing to do with any of the Test classes. They are ignored.Java Code:javac Foo.java
This will cause an error as the Foo class does not have a main method.Java Code:java Foo
This will successfully compile Test1 as the Foo class exists (assuming they are in the same folder).Java Code:javac Test1.java
This will successfully run the Test1 class which will create a foo object and call the doStuff method. "Hello world" will be displayed.Java Code:java Test1
Same as for Test1 class.Java Code:javac Test2.java java Test2
If you created another class that did not compile then you have some problems with it that may or may not be due to a main method. If we cannot see the code we cannot help.
- 05-11-2011, 04:11 AM #9
Member
- Join Date
- May 2011
- Posts
- 64
- Rep Power
- 0
I had no method in my third class what so ever, I tried calling a method on an object in the class body and my compiler wasn't interested.
eclipse tells me "Syntax error on token "printMe", Identifier expected after this token"Java Code:public class Test3 { Test tester = new Test(); tester.[U][COLOR="Red"]printMe[/COLOR][/U](); }
at the command prompt it says <Identifier> expectedLast edited by JohnPringle83; 05-11-2011 at 04:12 AM. Reason: spelling mistake
- 05-11-2011, 04:18 AM #10
Similar Threads
-
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
Calling The main method from another method
By SwissR in forum New To JavaReplies: 3Last Post: 07-27-2010, 11:03 AM -
calling method from main method
By bob_bee in forum New To JavaReplies: 4Last Post: 10-02-2009, 05:30 PM -
[SOLVED] Sorting / compareTo method confusion. Please help
By kbullard516 in forum New To JavaReplies: 8Last Post: 03-19-2009, 09:38 PM -
[SOLVED] calling a boolean method, confusion!!
By AngrYkIdzrUlE in forum New To JavaReplies: 18Last Post: 03-15-2009, 10:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks