Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes execution of main and static initialiser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "execution of main and static initialiser" Watch "execution of main and static initialiser" New topic
Author

execution of main and static initialiser

Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
without main , is it possible to define an alternate running path by starting a thread in static initialiser ?

Or can I delay the execution of main method ?
when i entered java Test1 it printed
"from test2
Exception in thread mainjava.lang.NoSuchMethodError: main"

class Test2 extends Test1{
static int x;

static{
x = 10;
System.out.println("from Test2");
ThreadTest th = new ThreadTest();
}
}

class ThreadTest extends Thread{

public void ThreadTest(){
start();

}
public void run(){
try{
while(true){
System.out.println("helloin new thread");
System.in.read();
}
}catch(Exception e){}
}
} :roll:
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
A JVM will look for a main() method (with the right signature) to start a Java application.
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
Java program will not run if the method main is not defined. Static blocks are called before main but you cannot start another thread in that.
[ July 23, 2002: Message edited by: Snigdha Solanki ]

Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Hi there,
You can not Bypass Main Method but u can delayed the excecution of
main method by creating static block
When class is loaded static blocks are executed first
then static methods and so on
Take a look at Following code
============================================
class Test2 {
static int x;
static{
x = 10;
System.out.println("from Test2");
ThreadTest th = new ThreadTest();
}

public static void main(String [] arg)
{
System.out.println("In Main");
}
}
class ThreadTest
{
public ThreadTest()
{
System.out.println("In Constructor");
}
}

============================

Ouput will be
from test2
In Constructor
In main
Now come to question that whether we can avoid main --NO
b'coz when u write
java <class file name>
first thing JVM look for is Main method although it is not executed first still
it is being looked first
hope it will clear some doubt

one more thing the code u have posted i think u mistaken given a return type to thread class constructor


If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Originally posted by Snigdha Solanki:
Java program will not run if the method main is not defined. Static blocks are called before main but you cannot start another thread in that.
[ July 23, 2002: Message edited by: Snigdha Solanki ]

Hi dear
i think ur skipping something there is nothing wrong in creating thread in static block
try to run following code
==========================================
class Test2 {
static int x;
static{
x = 10;
System.out.println("from Test2");
ThreadTest th = new ThreadTest();
}

public static void main(String [] arg){
System.out.println("In Main");
}
}
class ThreadTest extends Thread{
public ThreadTest(){
System.out.println("In Constructor");
start();
}
public void run(){
try{
while(true){
System.out.println("helloin new thread");
System.in.read();
}
}catch(Exception e){}
}
}
==================================

Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
Originally posted by yogesh sood:

Hi dear
i think ur skipping something there is nothing wrong in creating thread in static block
try to run following code


What I meant was that if you don't specify main in the program you cannot expect it to run even though you are starting a thread in static block. I agree with you that there is nothing wrong in creating a thread in the static blocs. Sorry if you misunderstood me.
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
No problem dear
anyway r u from india ?
 
 
subject: execution of main and static initialiser
 
Similar Threads
a simple question
Thread functioning help
instanceof question?
does static methods inherit ? what about private ?
Super class constructor calls derived class overridden method?