Menu
Topics Index
...
`

Static blocks classes and methods


If you need explanation Read this topic

If you need Answer Take test on this topic

Static blocks classes and methods
What will be the output of the following program?
public class MyBasicStaticMemberClass
{
    public static class MyStaticMemberExampleClass
    {
        public void printStatus()
        {
            System.out.println("Inside static member class");
        }
    }
    public static void main(String a[])
    {
        StaticMemberTestClass staticMemberTest = new StaticMemberTestClass();
        staticMemberTest.testMemberClass();
        System.out.println("Inside Main");
    }
    static
    {
        System.out.println("Static Block");
    }
}
class StaticMemberTestClass
{
        public void testMemberClass()
        {
            MyBasicStaticMemberClass.MyStaticMemberExampleClass myStaticMemberExample = new MyBasicStaticMemberClass.MyStaticMemberExampleClass();
            myStaticMemberExample.printStatus();
            System.out.println("Inside Class");
        }
}

A. Static Block
Inside static member class
Inside static member class
Inside Main
Inside static member class
Inside Class
B. Inside Class
Inside Main
Static Block
Inside static member class
C. Static Block
Inside static member class
Inside Main
Inside Class
D. Static Block
Inside static member class
Inside Class
Inside Main
E. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is D first static block will be executed it prints "static block" and then then it goes to  prints "inside static member class " and then it prints "inside class" and then it prints "inside main"

Posted by Ramya Reddy Addulla    2014-10-20 12:26:38


Ans is D...here we are using the concept of inner classes..java can contains class as it's member

we have static nested classes and non static nested classes

here we are dealing with static nested classes, by using the keyword static key we can make the classes as static class,these classes can contains static and non static members, inside the inner class we can access only static members of outer class we cant accss the non static members of outer class
by using the outer class name we have to create object for the inner class
example:
outer.inner i=new outer.inner();

here we have static block...it wil execute only once when the class first time loaded(wen object is created (or) wen static variable or mthod is called (or) using class.forName("classname") we can load the class..in static block we can use only static vaiables

here first static block is executed Bcz alredy main() is invokd by the JVM...
next we are creating the object for the StaticMemberTestClass and calling testMeberClass() in these method we are creating object for the inner class by using the outerclass name..and calling the method in inner class next we are printg "inside class"
in main we are printing inside main

here .class file for inner class wil genarate innerclass name follwed by outer class name

Outer$Inner.class

Posted by Uday Kumar    2014-10-20 15:09:36


This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-21 04:20:42


© meritcampus 2019

All Rights Reserved.

Open In App