• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading a main method five times? � Phew!

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This refers to a mock question ID:4101281 on the uCertify PrepKit

This question gave me a class with two main methods in it.
What I didn�t realise, was that one of the main methods had been overloaded.

I�ve now looked into overloading and played around with the code and realised I can have at least five main methods. I thought at first you could only ever have one.

Is there a main (parent � Mother of Main) method in the code or are they all main methods?

How can I tell which methods will produce results and which won�t?

How can I tell when a main method will not compile?

Lastly, why is MAIN not a keyword?

Any advice or discussion gratefully accepted. Thanks.

Here�s the code. The first part is straight from the practice question referred to above.

class maincheck

// These two main methods print

{
public static void main(String args[])
{
System.out.println("This is for String");
main(5);
}

public static void main(int x)
{
System.out.println("This is for Integer:");
}

// customizing from here onwards

// This should print 800, but doesn't - why?

public static void main(double y)
{
y = 800;
System.out.println(y);
}

// This doesn't print either - why?
public static void main()
{
System.out.println("I can't believe this compiles and runs!");
}

public static void main(float f)
{
System.out.println("This won't print");
}

// The result is:
// This is for String
// This is for Integer:
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

As you've found out, you can have as many "main" methods as you like. There's nothing special about methods called "main" as far as the Java language is concerned, which is why it's not a reserved word. All methods of that name will compile as long as they are syntactically correct, and they will all produce output if called. So I'm not sure what you mean by "this won't print" and "This should print 800, but doesn't - why?". How are you calling those methods?

The special feature is that if the "java" executable is used to launch a Java program, it will look for a method with signature "public static void main (String[])" which it will call after launch.
 
Kevin Foulger
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf for your welcome and your answers. I'm still finding my way around the JavaRanch.

I didn't realise I had to call these particular methods. What is puzzling me is that this piece of code prints some output:

public static void main(int x)
{
System.out.println("This is for Integer:");
}

But, for example, this piece of code doesn't have any output:

public static void main()
{
System.out.println("I can't believe this compiles and runs!");
}

The two methods (as all the moethods in my class) appear to be very similar. None of them seem to be getting called anywhere, yet the first two have output, but the others don't. Please could you briefly explain why? Thanks.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This method is called because its signature matches what the "java" command is looking for when launching a program:
This method is called because the previous method invokes it directly ("5" is an int):

public static void main (int x) {
System.out.println("This is for Integer:");
}


You can call the other methods by using data types that match their respective signatures, e.g. "main(5.0)" and "main(5.0f)". (This may lead to you scratching your head what the difference between "5.0" and "5.0f" is - have a look at the data type chapter of any Java tutorial for an explanation).
 
Kevin Foulger
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf.

Thanks. I understand now the calling business and the data types. I see that you can call the methods from either one place, or after each method.
5.0 is a double calling the double 800 main method and the 5.0f is a float calling the float f main method.

This now works fine. Thanks again.

class maincheck
{
public static void main(String args[])
{
System.out.println("This is for String");
main(5);
}

public static void main(int x)
{
System.out.println("This is for Integer:");
main(5.0);
}

// customizing from here onwards

public static void main(double y)
{
y = 800;
System.out.println(y);
main();
}

public static void main()
{
System.out.println("I can't believe this compiles and runs!");
main(5.0f);
}

public static void main(float f)
{
System.out.println("Now it prints!");
}

// The result is:
// This is for String
// This is for Integer:
// 800.0
// I can't believe this compiles and runs!
// Now it prints!

}

 
reply
    Bookmark Topic Watch Topic
  • New Topic