• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes

 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a study guide, I met the following question:-

package foo;

public class Outer {
public static class Inner {
}
}

Which statements are true?
A.An instance of the Inner class can be constructed with �new Outer.Inner()�
B.An instance of the Inner class cannot be constructed outside the package foo.
C.An instance of the inner class can only be constructed from within the outer class.
D.From within the package bar, an instance of the inner class can be constructed with the �new Inner()�
Answer: A

I tried the following code:-

import foo.Outer;
import static foo.Outer.Inner;

public class Test {
public static void main (String args []) {
new Inner();
}
}

and it worked, which suggests that the answer �D� could be correct too.
 
Charley Watson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, with certification exams, it is always important not to infer more than is stated. Since the question doesn't include the static imports you've added in your solution, there is a single correct answer to the question. Also, I think the current exam practice is to include the correct number of replies which apply. As in :

Which two of the following four are true:
 
Raef Kandeel
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code without the static import and it worked too.


import foo.Outer.Inner;

public class Test {
public static void main (String args []) {
new Inner();
}
}
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers,

are you sure what imports really do?

When you type
package bar;
import foo.Outer.Inner;
class XYZ(...)
new Inner();


it is just the same as if you wrote
package bar;
class XYZ(...)
new foo.Outer.Inner();

without importing. This is not what answer D demanded.


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic