aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can inner classes be private? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can inner classes be private?" Watch "Can inner classes be private?" New topic
Author

Can inner classes be private?

djroberts
Greenhorn

Joined: Sep 20, 2000
Posts: 6
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

Joined: Sep 20, 2000
Posts: 6
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

Joined: Sep 23, 2000
Posts: 3285
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Richard Huang
Greenhorn

Joined: Oct 02, 2000
Posts: 26
Thanks, Paul.
Can I ask why 'd' is wrong?
Ray Hsieh
Ranch Hand

Joined: Sep 20, 2000
Posts: 59
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?

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can inner classes be private?