Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Can you print something in Java without using main() method?

user-image
Question ajoutée par himanshu mittal
Date de publication: 2013/07/08

Yes, by using Static block we can print Ex: class A { static { Sop("A"); } } this will executes before main method

class Print { static { System.out.println("Without Main"); System.exit(0); } } using static block we can print anything without main().

kamal kanta Garai
par kamal kanta Garai , Software Developer , Inventla Solutions

Yea, We can print something without using main() method.That is possible using a static method.If we declare any static method JVM will give the first preference to the static method then the main method.

yeah we can print using static block

Hasan Mobarak
par Hasan Mobarak , Software Engineer (Java) , Fakir Knitwear's ltd

of course. by using Statice Block i can print any thing without main method. BUT not less then jdk1.7 version.

Mohammed Javeed
par Mohammed Javeed , R&D Engineer , Nokia Solutions and Networks Pvt Ltd

static {

System.out.println("Hello World");

}

This can be done by using Static block and then we have to call exit() method.

Preetam Mukherjee
par Preetam Mukherjee , Digital Interaction Advisor , [24]7.ai

class Temp{

        static{

                 System.out.println("My name is Preetam Mukherjee");

                 System.exit(1);

}

}

Abhishek C
par Abhishek C , Developer

yes,

By using the static block

 class A{

static {

System.out.println("static block ");

}

}

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?