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?

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.