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

Sun Test Question !!!!!!!!!!!

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello this question is from suns sample question
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.)
1)class InnerOne{
public static double methoda() {return d1;}
}
2)public class InnerOne{
static double methoda() {return d1;}
}
3)private class InnerOne{
double methoda() {return d1;}
}
4)static class InnerOne{
protected double methoda() {return d1;}
}
5)abstract class InnerOne{
public abstract double methoda();
}

The ans given is 3,5. i think 2 is also true? can someone clear my doubt?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer 1,2 and 4 does not work since they are all in some way defined static, thus they can not access the instance variable d1 in OuterClass.
 
parag bharambe
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello andreas krohn
i think that answer 4 is also proper. since it is legal to have static inner class. Also it is accessing private variable of enclosing class, so it should work.
Can you give more light on it?
Parag
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Parag,
Answer 4 is incorrect since the inner class define with static .Static innerclasses cannot access non-static variables of it's enclosing class.

Answer 3 is legal because it can access the private variable d1 .
Answer 5 is legal because the inner class is define as a abstract class & contain only a abstract method.
All the other answers are define with static keyword & they cannot access the variable d1.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have some opinions.
What follows makes references to: Java Language Specification 2nd ed. (http://java.sun.com/docs/books/jls/second_edition/ html/j.title.doc.html).

8.1.2 states, "An inner class is a nested class that is not explicitly or implicitly declared static...." [see below for continuation of quote]
[For Reference: (Ch. 8, 2nd paragraph) "A nested class is any class whose declaration occurs within the body of another class or interface...."]
8.1 ("Class Declaration"), 8.1.1 ("Class Modifiers"), and the above (8.1.2) lead to this implication:
-->A nested class with the modifier "static" in its declaration is a nested class "explicitly ... declared static" and hence not an "inner class".
==> This would disqualify choice 4.

Continuing with above quote at 8.1.2 "... Inner classes may not declare static members, unless they are compile-time constant fields." (Spec follows this with sample code to clarify.)
==> Choices 1 and 2 would constitute declarations with "static members" and hence are not inner classes.
==> I can't find anything wrong with 3 or 5.

jl
ps. Did this make sense? Is it correct? I did not know how to quote more while keeping things clear... search for the Java Language Specification 2nd ed. at Sun's site.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic