Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
 
Legend has it that if you rub the right tiny ad, a genie comes out.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic