Showing posts with label Main Method. Show all posts

Which of the following valid main() method declarations?

public static void main(String args) Invalid
public static void Main(String[] args) Invalid
public void main(String[] args) Invalid
public static int main(String[] args) Invalid
final synchronized strictfp public static int main(String[] args) Invalid
static final public synchronized strictfp void main(String[] args) Valid
public static void main(String... args) Valid
Read more »

Can we execute java program without main() method?

class MainMethod6
{
static
{
System.out.println("static block");
}
}
/*
Output:- Depending upon java version.
Explanation:- Yes we can execute a java program without main() method with the help of static block
but this idea will work upto jdk1.6 version. Still after printing output in jdk1.6 it
will generate an exception saying "NoSuchMethodError: main". We can hide this exception
by writing "System.exit(0)" before closing the static block. But after from jdk1.7
onwards main() method is mandatory eventhough we are defining static block if the class
doesn't contain main() method it will generate Runtime Error saying "Main method not
found in class MainMethod6, please define main method as......"
*/

Read more »

Can we override main() method?

class A
{
public static void main(String[] args)
{
System.out.println("parent class main() method");
}
}
class MainMethod5 extends A
{
public static void main(String[] args)
{
System.out.println("child class main() method");
}
}
/*
Output:- parent main() method
Explanation:- It seems overriding concept applicable for main() method but it is not method overriding,
it is method overhiding. If we execute class A,then A class main() method will be executed
and if we execute MainMethod5 class main() method then it will execute its main() method.

*/

Read more »

What is the output if we execute MainMethod4 class?

class Test
{
public static void main(String[] args)
{
System.out.println("parent main() method");
}
}
class MainMethod4 extends Test
{
}
/*
Output:- parent main() method
Explanation:- Inheritance concept is applicable for main() method. Hence while executing child class
if child class doesnot contain main() method then parent class main() method will be                           executed.
*/
Read more »

Can we overload main() method?

class MainMethod3
{
public static void main(String[] args)
{
System.out.println("String[] args");
}
public static void main(int[] args)
{
System.out.println("int[] args");
}
}
/*
Output:-
Explanation:- Yes we can overload main() method but JVM will always call "String[]" argument
main() method only. The other overloaded method we have to call explicitly then
it will be excuted just like a normal method call.
*/
Read more »

Can we interchange the modifiers used with main() method?

class MainMethod2
{
static void public main(String[] args)
{
System.out.println("Can we interchange the modifiers?");
}
}
/*
Output:- Compilation error
Explanation:- Yes we can interchange modifiers of main method but cannot interchange return type.
There are 11 modifiers in which we can use 5(public,static,final,synchronized,strictfp)
modifiers with main() method. Excpet these if we are trying to use other modifier then                         we will get compilation error. We can write like this:-
static public void main(String[] args)
static final public synchronized strictfp void main(String[] args)
*/
Read more »

Is this a valid java program?

class MainMethod1
{

}
/*
Output:- No compilation error
Explanation:- Yes, it is valid Java Program.Whether the class contains main() method or not and
whether main() method is properly declared or not. Thses things won't be checked by
compiler. At runtime JVM is responsible to check these things. At runtime if JVM is
unable to find required main() method then we will get Runtime Exception saying
"NoSuchMethodError:main" on jdk 1.6 version. If we are using jdk 1.7 or greater
version then we will get Runtime Error saying "Main method not found in class
MainMethod1, please define the main method as:
public static void main(String[] args)"
*/
Read more »