Results 1 to 10 of 10
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default 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?

  2. #2
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default 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();
    	}
    }
    Java Code:
    public class Test2 {
    	public static void main(String[] args){
    		Test tester = new Test();
    		tester.printMe();
    	}
    }
    I tried something else with this and found something very interesting:

    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?

  3. #3
    Kratos321's Avatar
    Kratos321 is offline Member
    Join Date
    May 2011
    Posts
    31
    Rep Power
    0

    Default

    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    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:
    when a program is compiled, that the JDK looks for a main method and starts from there.
    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.
    Execution

    db
    Last edited by DarrylBurke; 05-11-2011 at 03:18 AM. Reason: Added link

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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:
    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();
        }
    }
    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by JohnPringle83 View Post
    when a program is compiled, that the JDK looks for a main method and starts from there.
    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.

  7. #7
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You are still confused about compiling and running. Using the code in my above example:
    Java Code:
    javac Foo.java
    This will successfully compile the Foo class. It has nothing to do with any of the Test classes. They are ignored.
    Java Code:
    java Foo
    This will cause an error as the Foo class does not have a main method.
    Java Code:
    javac Test1.java
    This will successfully compile Test1 as the Foo class exists (assuming they are in the same folder).
    Java Code:
    java Test1
    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:
    javac Test2.java
    java Test2
    Same as for Test1 class.

    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.

  9. #9
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default

    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.

    Java Code:
    public class Test3 {
    	Test tester = new Test();
    	tester.[U][COLOR="Red"]printMe[/COLOR][/U]();
    }
    eclipse tells me "Syntax error on token "printMe", Identifier expected after this token"

    at the command prompt it says <Identifier> expected
    Last edited by JohnPringle83; 05-11-2011 at 04:12 AM. Reason: spelling mistake

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You cannot have code floating about like that. It has to be inside a method or constructor or initialiser block. Ignore that last one for now. If you are stuggling with main methods then initialiser blocks are too advanced.

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 11:03 AM
  3. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 05:30 PM
  4. Replies: 8
    Last Post: 03-19-2009, 09:38 PM
  5. [SOLVED] calling a boolean method, confusion!!
    By AngrYkIdzrUlE in forum New To Java
    Replies: 18
    Last Post: 03-15-2009, 10:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •