Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a class be instantiated as static in java? [duplicate]

Tags:

java

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

like image 586
keshav kashyap Avatar asked Nov 04 '12 12:11

keshav kashyap


People also ask

Can static classes be instantiated in Java?

A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class.

Can we instantiate static class?

A static class cannot be instantiated. All members of a static class are static and are accessed via the class name directly, without creating an instance of the class. The following code is an example of a static class, CSharpCorner.

Can static members of a class have multiple copies?

Static variables in a class: As the variables declared as static are initialized only once as they are allocated space in separate static storage so, the static variables in a class are shared by the objects. There can not be multiple copies of same static variables for different objects.

Why static classes Cannot be instantiated?

It is not possible to create instances of a static class. Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class.


3 Answers

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.)

like image 180
Hot Licks Avatar answered Sep 29 '22 00:09

Hot Licks


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.

like image 45
Stephen C Avatar answered Sep 28 '22 00:09

Stephen C


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.

like image 43
Peter Lawrey Avatar answered Oct 01 '22 00:10

Peter Lawrey