You only need a static main method. When Java compiles and executes, it will look for a static method called main inside of the public class. If found, it will be executed.
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String args)
{
System.out.println("Lisa","A.","Smith");
} // main
} // public class
Hi,
I have to write a method that returns my first name, middle initial, and last name as a single String and call the method in java. So far, I have:
public class ReturnName {
static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
}
}
I don't know where to go from there.
2 answers
I come around to test the above program, and found two corrections,
- String[] args replaces String args in the declaration of the main method.
- forgot to invoke the fullname method in the System.out.println() call.
The private declaration of fullname is optional, but helps to keep a clean namespace.
Here's the corrected version.
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String[] args)
{
System.out.println(fullname("Lisa","A.","Smith"));
} // main
} // public class
- String[] args replaces String args in the declaration of the main method.
- forgot to invoke the fullname method in the System.out.println() call.
The private declaration of fullname is optional, but helps to keep a clean namespace.
Here's the corrected version.
public class ReturnName {
private static String fullname(String firstname, String middleinitial, String lastname)
{
return firstname + " " + middleinitial + "." + " " + lastname;
} // fullname
public static void main(String[] args)
{
System.out.println(fullname("Lisa","A.","Smith"));
} // main
} // public class