954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Which type of class is not instantiated?

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?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You