1.) if we use "private" in place of "public" in public
static void main()...
2.) if we use "int" in place of "void"....
3.) can we ommit "static" keyword from that statement....
4.) also can we give the command line arguments type as int
type or float,etc.(.i.e (string args[]))

Answer Posted / ankur pandya

Hello.
the sign for main method in Java is
public static void main(String arg[])
or
public static void main(String[] arg)

Because, "public" modifier says this method is accessible
from out side the class, "static" says that this is not
instance method. Means method is of class and not of any
object of class. "void" means this method does not returns
any thing.
and "String[] arg" says the method take the argument of type
Sting Array, so that we can supply argument of any type (and
cast it later on) and in any quantity. String is only option
to do so in Java ;) .

Now

1. if we use "private" in place of "public" than compiler
will not generate any error. But while running this program
we get "Main method not public". Hence Main method become
unreachable.

2. if we use "int" in place of "void", then compile will not
generate any error but run time error saying
"java.lang.NoSuchMethodError: main" because we can not
overload "main" method.

3. if we omit "static" keyword then same runtime error
"java.lang.NoSuchMethodError: main" will display, because we
can not override "main" method. If it is not "static" then
all of its object can override its definition, which is not
good for a program.

4. if we give the command line arguments type as int
type or float,etc.then same runtime error
"java.lang.NoSuchMethodError: main" will display, because we
can not overload "main" method. The program should have only
one beginning of a program. If you want to supply any int or
float then you'll have to use type casting. Usually we do
not supply any argument, in this case compiler will
understand arg[] = null.

Is This Answer Correct ?    16 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we store variables in local blocks?

780


What is factor r?

516


Explain the differences between static and dynamic variables?

578


List out five keywords related to exception handling ?

596


What are heap memory and stack memory and what are memory tables.

505






What are functions in java?

496


Why array is used in java?

509


Which sort is best in java?

522


What are thread groups?

566


What is return code?

556


What is singleton class and how can we make a class singleton?

657


What are the advantages of arraylist over arrays?

559


The following program reads data (details of students) from a file named students.txt and converts it into e-mail addresses. The results are written to a file named studentemail.txt. students.txt consists of a number of lines, each containing the data of a student in colon delimited format: Last Name:First Name:Student Number Each input record is converted to an e-mail address and written to studentemail.txt in the following format: the first character of the last name + the first character of the first name + the last four digits of the student number + “@myunisa.ac.za” import java.io.*; public class EmailConverter { public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader(new FileReader ("students.txt")); PrintWriter output = new PrintWriter(new FileWriter ("studentemail.txt")); String line = input.readLine(); while (line != null) { // Extract the information for each student String[] items = line.split(":"); // Generate the email address String email = "" + items[0].charAt(0) + items[1].charAt(0) + items[2].substring(4,8) + "@myunisa.ac.za"; email = email.toLowerCase(); // Output output.println(email); line = input.readLine(); } input.close(); output.close(); } } Rewrite the class so that it handles possible errors that may occur. In particular, it should do the following: • It should catch at least three appropriate exceptions that might occur, and display suitable messages. • At this stage, the program will not run correctly if there is an empty line in the input file. Change the program so that if an empty line is encountered, an exception is thrown and the empty line is ignored. This exception should be handled with the display of a suitable error message. • Before the e-mail address is added to the output file, check if the student number has 8 digits. If not, throw an InvalidFormatException (which the program should not handle itself)

1449


How does split work in java?

528


How to retrieve data from database in java using arraylist?

522