can we create a object in static block
class A
{
static {
A a=new A();
}
}

Answer Posted / kaushal mittal

Yes we can create a object in static block.
1- if there is no main method than it will compile easily but at run time it will throw exception
//Exception in thread "main" java.lang.NoSuchMethodError:
main
2- if we put main method inside the class then it will compile and run successfully and we will get object.

Is This Answer Correct ?    10 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is actionlistener in java swing?

733


What is difference between awt and swing?

775


Who created the swing?

684


Why does JComponent have add() and remove() methods but Component does not?

739


What are swing components?

797


How to align components in java swing?

795


What does javax swing do?

715


What are heavy weight components ?

799


What are the components of swing?

734


What are the benefits if Swing over AWT?

788


Which window contains the swing controls?

709


Is javafx better than swing?

715


What are the two key features of swing?

709


What is swing in java javatpoint?

713


Why swing components are called lightweight components?

695