• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question from Mughal/Rasmussen book

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm new to the board and like many I plan to take the exam soon.
I had some doubts about question 7.2 on page 236 in the book regarding inner classes:
The question is:
Which of these statements concerning nested classes are true?
Select all valid answers.
(a) An instance of a top-level nested class has an inherent outer instance.
(b) A Top-level nested class can contain non-static member variables
(c) A Top-Level interface can contain non-static member variables
(d) A Top-leve nested interface has an inherent outer instance.
(e) For each instance of the outer class, there can exist many instances of non-static inner class.
The answer given is B, E
I thought that C should also be valid, but the authors state in the answers that "A top-level nested interface, just like package member interfaces, cannot contain non-static member variables"
However, upon testing I was able to create a top-level nested interface, declare a non-static variable (int a), and call it from an instance of a class that implements the nested interface:
public class TopClass {

public static class TopLevelNestedClass {
public static interface TopLevelNestedInterface {
int a = 10;
}

public static class NestedNestedClass implements TopLevelNestedInterface {
}
}
static public void main(String args[]) {
TopClass.TopLevelNestedClass.NestedNestedClass objInner = new TopClass.TopLevelNestedClass.NestedNestedClass();
System.out.println ("int a = " +objInner.a);
}
}

I'm scratching my head here- did I not understand the question correctly?
Thanks,
Charles
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because any variables declared inside a interface are automatically static and final.
Thinking in java, Bruce eckel, pg: 275
 
Charles Watson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sridevi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic