6

Possible Duplicate:
Java: Static Class?

Can a class be instantiated as static in java?

static class c1(){

}

Can this be done? I'm confused with this and memory mapping with non static stuff's. Please help

Community
  • 1
  • 1
keshav kashyap
  • 133
  • 2
  • 9

5 Answers5

10

The significance of static with a class definition is not whether the class can be instantiated or not, but rather whether the class must be instantiated from within a non-static method of the outer class or not.

Non-static inner class instances are tied to the instance that created them -- there's a pointer in the inner class instance back to the creating instance (which is useful in a number of ways). Static inner class instances are not tied to the creating instance.

(I worked in the innards of the JVM for about 10 years and I still find this confusing.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
2

Can the class be instantiated with the static keyword in java ? eg : static class c1(){ }

Your terminology is incorrect. "Instantiating a class" means creating an instance of the class; i.e. creating an object. This is done using the new operation. Your example is really about declaring a class.

Having said that, yes you can declare a class as static, but this can only done for a nested class; i.e. a class declared inside another class.

am confused with this and the memory mapping with non static stuff's please help

I haven't a clue what you are talking about here. If you need more help on this point, you will need to explain yourself more clearly.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

static doesn't have anything to do with memory mapping. It means there is no instance it is associated with.

For a static class it means instances of the class are not associated with an outer class instance.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

It breaks the paradigms. Consider this one: Static members are members that every instance has in common, but its not clear how to extend this idea of shared to a class?

What did you expect the static keyword to do?

Mikhail
  • 7,749
  • 11
  • 62
  • 136
0

You can have static class as shown below

class A
{
static class B   //static inner class
    {
     static void dis()
      {
      System.out.println("this is me");
      }
    }
}

class four extends A.B
{ 
    public static void main(String args[])
    {
    dis();

    }
}
Abhishekkumar
  • 1,102
  • 8
  • 24