Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by himanshu mittal
Date Posted: 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().

Yes but before in version 1.6

kamal kanta Garai
by 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
by 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
by 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
by Preetam Mukherjee , Digital Interaction Advisor , [24]7.ai

class Temp{

        static{

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

                 System.exit(1);

}

}

yes,using static block we can.

Abhishek C
by Abhishek C , Developer

yes,

By using the static block

 class A{

static {

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

}

}

More Questions Like This