• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Can inner classes be private?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Enthuware Software Support
Posts: 4667
51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Paul.
Can I ask why 'd' is wrong?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic