| | |
Which type of class is not instantiated?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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?
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?
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by Narue
>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.
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:
But this is not:
The difference is that a static class is somewhat like a static data member. An instance isn't required to access it.
Huh? No, not at all. The following is legal:
Java Syntax (Toggle Plain Text)
class Outer { static class Nested {} } class Test { Outer.Nested obj = new Outer.Nested(); }
Java Syntax (Toggle Plain Text)
class Outer { class Nested {} } class Test { Outer.Nested obj = new Outer.Nested(); }
I'm here to prove you wrong.
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).
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).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
![]() |
Similar Threads
- Abstract class homework problem (C++)
- STL priority_queue with custom type...how do I do iit (C++)
- Element is undefined in a Java object of type class [Ljava.lang.String; referenced as (ColdFusion)
Other Threads in the Java Forum
- Previous Thread: parsing error (XML file-java-schema)
- Next Thread: Please Help Me
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






