Sunday, June 2, 2013

Basic Interview Question in Java



Introduction

Java, now it’s time to play with JAVA. I started with a new existing point.

Can we use multiple main in a program

Yes; we can use multiple “main()” method in a java program. But the both main methods argument should not be same, as you know, the  java programs execution is start from “main()” method means (
public static void main(String arg[]), then simply declare the second ‘main()” method in this main method for execution, Now the given below example shown you how to do it.

E
xample

 class A
{
public static void main(String arg[])
{System.out.println("welcome");
main();

}
public static void main()
{
System.out.println("first");
}
}

Output

Can we run the program without main(String [] args) method

Yes we can run a java program without  main() method. Because java provide a functionality with the use of “static” keyword and that “static” keyword can be a block or static data member or static method,  firstly execute whenever you compile a  java program. Let’s see, it works or not.

Example
 class A
{
static
{
System.out.println("welcome");
System.exit(0);
}
}Output



Can we change the main thread name?

Yes, we can change the main thread name.

Can we print the message before the main method execution?

Yes, we can print the message before the main method. Let’s see how

Example
class A
{
static
{
System.out.println("welcome");
}
public static void main(String arg[])
{System.out.println("main method");}}Output