could you run the java program without main method?

Answers were Sorted based on User's Feedback



could you run the java program without main method?..

Answer / amit singh

yes you can run java peogram without main
we do it through the static block ,because its execute
before the main method,
so when the class is loaded it will execute before main
but whrn the static block excute and after exiting it jvm
search for main when it will not fou8nd it then it throw
some exception so for this we use the
System.exit();
class Moon
{
static
{
System.out.println("hello i am visible without main");
}
System.exit();
}
Thanks amit singh

amitsing2008@gmail.com

Is This Answer Correct ?    36 Yes 8 No

could you run the java program without main method?..

Answer / amit singh

please put the System.exit(0) in static block thanks
class Moon
{
static
{
System.out.println("hello i am visible without main");
System.exit();
}
}
amit singh
amising2008@gmail.com

Is This Answer Correct ?    29 Yes 8 No

could you run the java program without main method?..

Answer / a.d. vijaykumaran

Yes. static is powerful word in java because of within
static block inside we can define statement and then
System.exit(0); use this to avoid runtime error main thread
exception error.
class Beatme //we can use any abstract class or interface
{
static // we can't do throws Exception
{
System.out.println("A.D.Vijaykumaran");
System.exit(0);
}
}

and one disadvantage above the program as doesn't
define "System defined Exception r throws Exception" .

Is This Answer Correct ?    13 Yes 2 No

could you run the java program without main method?..

Answer / shaik baji

Yes, we can execute the java program with out the main
method by using the the static block. After executing the
static block the JVM will search for the main method so to
skip this we should call System.exit(int) method.

NOTE: System.exit method contains integer parameter so we
shoud pass the integer value.
class MainDemo
{
static
{
System.out.println("hello i am visible without
main");
System.exit(0);
}

}

Is This Answer Correct ?    14 Yes 4 No

could you run the java program without main method?..

Answer / naga

yes,we can write and run a java program without using the
main method.
i.e By using static

example program...!

public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}

Is This Answer Correct ?    23 Yes 22 No

could you run the java program without main method?..

Answer / rammohan

with out main() and static block also run java program
through static obj creation
class nomainnostatic
{
nomainnostatic()
{
System.out.println("vaaniMohan");
System.out.println("hai");
System.exit(1);
}
static nomainnostatic n=new nomainnostatic();
};
support some jdk versions only
exp:when ever static obj is created the default constructor
will be called,so the constructor block will be executed.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are keywords in java?

0 Answers  


Does java set allow duplicates?

0 Answers  


These static constructors are correct ? class A { statc intA() { } static A(int x,int y) { } static A(int x) { } }

0 Answers   HCL,


What is navigable map in java?

0 Answers  


What is primitive data type in java?

0 Answers  






What do you understand by weak reference?

0 Answers  


What is arraylist class in java?

0 Answers  


What is a Java package and how is it used?

2 Answers  


What is comparable and comparator interface? List their differences

0 Answers  


Name two subclasses of the TextComponent class?

2 Answers  


What is the reason that multiple inheritance is not possible in java??

1 Answers  


Where is the find and replace?

0 Answers  


Categories