This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 4588
45
  • 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?


 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic