Xelian
Xelian

Reputation: 17208

Why can't create instance of local class in Java?

If I have this code.

public class Test{
        {
            class People {

            }
        }

        public static void main(String[] args) {
            People person = new People();//Compile ERROR
        }

    }

I can't create instance of People.

Does that mean Initializer Block can't be used for defining classes?

Upvotes: 2

Views: 5231

Answers (5)

Bizmarck
Bizmarck

Reputation: 2688

You can do

Test t = new Test();
People person = t. new People();

Because People otherwise has no enclosing instance

EDIT:

Since the class definition is in an initialization block, it is only visible and useable locally, INSIDE the initialization block (which makes it a peculiar place to define a class). The People class can't be visible in main since there is no visible type for People. The approach I outlined above would only work if there were no initialization block around the class definition.

Upvotes: 1

PNS
PNS

Reputation: 19895

The initializer block (defined by the extra brackets) changes the scope of the declared class, so it will not be visible outside, much like a variable would not be. So, the only place where a People object can be created, is within the block itself, e.g.

{
   class People {
   }

   People person = new People();
}

Without the initializer block, there are 2 options you can follow:

  1. Declare the People class as static
  2. Create a Test object and then instantiate a People object

In the first case, the only thing that changes is the line

static class People

For the second case, the main method would look something like

public static void main(String[] args) {
    People person = new Test().new People();
}

Upvotes: 1

Kick
Kick

Reputation: 4923

Yes we can declare initializer block and scope of the class People will be within it.Update the code as below :

  public class Test {
    {
        class People {  
             /// Stuff          
        }

        People people = new People(); // Create object of People
        System.out.println("Initializer block");
    }

    public static void main(String[] args) {
        Test obj = new Test();
    }
}

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500105

(Adding a second answer as my wrong one had three upvotes already.)

You can declare a class within an initializer block - but its scope is that initializer block, just like if you declared it in a method. So this works:

public class Test {
    {
        class People {            
        }

        People people = new People();
        System.out.println("In initializer block!");
    }

    public static void main(String[] args) {
        new Test();
    }
}

I can't say I've ever done this before though, nor can I imagine myself wanting to do it any time soon. Note that this is a local class, which is always implicitly an inner class. (Although if you declare a local class in a static method, there's no enclosing instance, just to make the terminology slightly weird...)

If you want to create a class which code outside the initializer block can "see", you need to declare it outside the initializer block. (Just like any other declaration - if you declared a variable within the initializer block, you wouldn't expect to be able to see it outside would you?)

Upvotes: 6

P45 Imminent
P45 Imminent

Reputation: 8591

You need to make the inner class static:

static class People {

Upvotes: 2

Related Questions