ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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

user-image
تم إضافة السؤال من قبل himanshu mittal
تاريخ النشر: 2013/07/08

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

Ashish Gupta
من قبل Ashish Gupta

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

kamal kanta Garai
من قبل 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.

Hasan Mobarak
من قبل 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
من قبل 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
من قبل Preetam Mukherjee , Digital Interaction Advisor , [24]7.ai

class Temp{

        static{

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

                 System.exit(1);

}

}

Abhishek C
من قبل Abhishek C , Developer

yes,

By using the static block

 class A{

static {

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

}

}

المزيد من الأسئلة المماثلة