1)What is the output for below code?
class Testing {
{
System.out.println(" block");
}
public static void main1(String[] args) {
// TODO Auto-generated method stub
System.out.println("Main method");
}
}
Ans: Print notning as the no main method, but runs sucessfully witout error.
2) What is the output for below code?
class Testing {
{
System.out.println(" block");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Main method");
}
}
Ans: prints Main method in console as there is no chance to call instance block
3)What is the output for below code?
public class Test {
static
static
Main method
instance
Constructor
3)What is the output for below code?
public class Test {
Test(){
System.out.println("Constructor");
}
static {
System.out.println("static ");
}
static {
System.out.println("static ");
}
{
System.out.println("instance ");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Main method");
Test obj=new Test();
}
}
Ans: we can any nuber of statis blocks, no structural issue, it prints static
static
Main method
instance
Constructor
0 comments: