This is kind of a weird senario. This question was at the java state championship I was at a few weeks ago. Here was the question:

Which type of class cannot be instantiated:

abstract
static
(choices I can't remember..they were wrong)

Would both abstract and static be correct answers? I've never heard of static classes, but if there is such a thing, wouldn't it be the Dot operator that calls it, and not an instantiation?

Dani AI

Generated

A short, exam-ready clarification and a few useful edge notes that expand on what , and discussed.

The canonical answer to "which class cannot be instantiated" is an abstract class (it is a compile-time error to try to create an instance of an abstract class). JLS §8.1.1.1. Two other language-level kinds that are not directly instantiable are interfaces (they "may not be directly instantiated") and enum types (an enum has no instances except its declared enum constants; explicit or reflective instantiation is prohibited). JLS §9. JLS §8.9.

Practical nuance: an anonymous class expression creates a concrete, unnamed subclass at the point of use, so you can write an inline implementation for an interface or an abstract class and get an instance of that anonymous subclass (the anonymous class itself is never abstract). See the JLS rules for class instance creation / anonymous classes. JLS §15.9.

Quick runtime note: trying to instantiate an abstract class or interface via reflective newInstance-like calls will fail with an InstantiationException; enums are additionally protected from reflective construction. java.lang.InstantiationException javadoc. Also, as and clarified, the static modifier applies only to nested types (there is no top-level static class), so a static nested class is instantiable like any normal (non-abstract) class — it just doesn’t require an enclosing instance. JLS §8.1.1.4 / §8.1.3.

Recommended Answers

All 8 Replies

>Would both abstract and static be correct answers?
No, abstract is the correct answer. static only means that a nested class doesn't designate an inner class.

>Would both abstract and static be correct answers?
No, abstract is the correct answer. static only means that a nested class doesn't designate an inner class.

OHHH, ok. I see, the actually main class of the static nested class would also have to be static, which isn't possible. In that case, you would be instantiating a class to get to the static..Correct?

Thanks for the reply by the way.

>In that case, you would be instantiating a class to get to the static
Huh? No, not at all. The following is legal:

class Outer {
  static class Nested {}
}

class Test {
  Outer.Nested obj = new Outer.Nested();
}

But this is not:

class Outer {
  class Nested {}
}

class Test {
  Outer.Nested obj = new Outer.Nested();
}

The difference is that a static class is somewhat like a static data member. An instance isn't required to access it.

Well how come you couldn't consider that an answer?

>Well how come you couldn't consider that an answer?
Because your explanation was so convoluted I didn't understand it, but it didn't smell right. ;)

I was thinking since you have to call the outer class that you couldn't count it as the answer...Is this what you were meaning?

You can create instances of a static inner class, but not of an abstract class.

When you have a static inner class the Class instance for the inner class is static, but that has nothing to do with any instances of the inner class itself.

Consider an inner class to be a data member of the outer class.
In case of non-static inner classes you'll get a Class instance for the inner class for each instance of the outer class.
When you have a static inner class you'll get a single static Class instance at Class level for the outer class, therefore accessible without an instance of the outer class itself (enabling you to do new A.B() instead of new A().new B() for example).

Ahh I get it now. Thanks for the clarification jwenting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.