can we create a object in static block
class A
{
static {
A a=new A();
}
}
Answers were Sorted based on User's Feedback
Answer / 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 |
Answer / anjani kumar jha
Yes We can do..........................there is no problem
static
{
FileSize fz=new FileSize();
System.out.println(fz);
}
Is This Answer Correct ? | 9 Yes | 0 No |
Answer / srinu
Yes we can create a object in static block
EX:-
public class Stobject
{
static
{
Stobject st =new Stobject();
}
Stobject()
{
System .out.println("object created");
}
public static void main(String k[])
{
System .out.println("HAI");
}
}
output:-
object created
HAi
Is This Answer Correct ? | 6 Yes | 0 No |
Answer / naresh tuhania
Yes we can do this . Because we can create an object at
runtime and static block is also execute when a class is
loaded by the class loader at runtime
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / karthik
We can create object because static block is a executable
block and we don't want any Instance to call. It will be
called before executing or calling the main thread
But here we will get object and exception
//Exception in thread "main" java.lang.NoSuchMethodError:
main
Is This Answer Correct ? | 6 Yes | 6 No |
What are the features of swing in java?
Difference between AWT and Swing compenents?
What are the advantage of swing over awt?
What does x mean in javax.swing?
What are the different components that are used to show a container?
What are the different types of layout managers used in swing?
What is the difference between applications and applets?
What is swing control?
How to create a swing gui in java?
How to generate bill in java swing?
What is the use of swing in java?
What is sling swing?