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

Scjp help

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class OuterClass {
2. private double d1 = 1.0;
3. // insert code here
4. }
Which two are valid if inserted at line 3? (Choose two)
A. static class InnerOne {
public double methoda() { return d1; }
}
B. static class InnerOne {
static double methoda() { return d1; }
}
C. private class InnerOne {
public double methoda() { return d1; }
}
D. protected class InnerOne {
static double methoda() { return d1; }
}
E. public abstract class InnerOne {
public abstract double methoda();
}
Answer: C, E.
Why ?
Thanks!
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A, B and D are wrong because

static inner class methods can only access static members of the outer class.

someone correct me if i am wrong.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A is an attempt to create a static nested class. A "static class" within another class is not really considered an inner class. It's legal, but a static nested class certainly has no access to instance variables in the outer class.

B is also a static nested class, and has no access to instance variables in the outer class.

The notion of an instance of the outer class has no meaning in A and B because a static nested class is tied to the outer class rather than an instance of that class. (thanks Marc for pointing that out to me )

A nested class is is instantiated with:

Its association with the outer class is restricted to being able to access class (static) variables of the outer class, but not instance variables (fields).

C, D, and E are attempting to be inner classes. D fails during compile time because an inner class cannot have static members.

C is a nice well-behaved inner class. And E is OK because it's all right for an inner class to be abstract.

As for the qualifiers on the "class" declarations. A class declared within an outer class but outside a method can have have the following access specifiers: public, protected, private (or none at all). This makes it an inner class. If it has a modifier of static in its class declaration, it's a static nested class, and that's legal too.

(jls 8.1.2 Inner Classes and Enclosing Instances
An inner class is a nested class that is not explicitly or implicitly declared static... Inner classes may not declare static members, unless they are compile-time constant fields ...Inner classes may inherit static members that are not compile-time constants even though they may not declare them. Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language.

*whew*, I don't envy Kathy and Bert's job
[ September 17, 2004: Message edited by: Louie van Bommel ]
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I know the "D fails during compile time because an inner class cannot have static members.C is a nice well-behaved inner class. And E is OK because it's all right for an inner class to be abstract."
But I compile the A and B,it's right ,why ?? So I think A and B is also legal.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by PETER CARTER:
...I compile the A and B, it's right...


Does this code (A) compile for you?

I get the following error (as expected): "OuterClass.java:5: non-static variable d1 cannot be referenced from a static context"
[ September 12, 2004: Message edited by: marc weber ]
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,weber :Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic