• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can inner classes be private?

 
djroberts
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following question, c and e are correct. I thought classes could not be declared private? In fact the Exam Cram book states this in the cram sheet? What am I missing? I can only assume that inner classes support private...
2. Given:
public class OuterClass {
private double d1 = 1.0;
//insert code here
}
You need to insert an inner class declaration at line 3. Which two inner class declarations are valid?(Choose two.)
class InnerOne{
public static double methoda() {return d1;}
}
a. public class InnerOne{
static double methoda() {return d1;}
}
b. private class InnerOne{
double methoda() {return d1;}
}
c. static class InnerOne{
protected double methoda() {return d1;}
}
d. abstract class InnerOne{
public abstract double methoda();
}
 
djroberts
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
the letters are one off. a is the first one and e is the last one like follows:

a. class InnerOne{
public static double methoda() {return d1;}
}
b. public class InnerOne{
static double methoda() {return d1;}
}
c. private class InnerOne{
double methoda() {return d1;}
}
d. static class InnerOne{
protected double methoda() {return d1;}
}
e. abstract class InnerOne{
public abstract double methoda();
}
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3776
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member inner class whether static or non static, ( ie. inner class declared in class scope ) can have private as well as protected access modifiers. The reason is such classes are kind of equivalent to other members ( fields and methods ) of the class.
But local inner class ( ie. inner class defined in method) cannot have any access modifier. This is again consistent with Java's treatment of local variables.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
http://www.enthuware.com/jqplus
 
Richard Huang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Paul.
Can I ask why 'd' is wrong?
 
Ray Hsieh
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cos in d the inner class is static, Richard. And there's no way it can figure out which instance variable it should refer to in the return statement.
It's just like how the good ol'
public static void main(String[] args)
can't access your non-static members.
Cheers
Originally posted by Richard Huang:
Thanks, Paul.
Can I ask why 'd' is wrong?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic