• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQ+ Inner Class Question

 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This one is getting the better of me:
--------------------------------------
Which of the following statements regarding inner classes is true?
Select all 3 correct choices:
1. A non static inner class may have static members.
2. Anonymous inner classes cannot have any 'extends' or 'implements' clause.
3. Anonymous inner classes can only be created for interfaces.
4. Anonymous inner classes can never have initialization parameters.
5. Anonymous inner classes cannot be static.
---------------------------------------------------
There are 3 correct choices?
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the 2,4,5 is the answer
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranjan
I'll say 2, 4, and 5 are true.
1 is not true, the jls says --
Inner classes may not declare static members, unless they are compile-time constant fields.

2 is true because an anonymous inner class has just the name of the class it extends or the interface it implements where it is defined and created.
3 is false because an anonymous inner class can extend another class.
5 is also true becasue an anonymous inner class cannot be static, you can't use any modifiers on it in its declaration and creation.
By process of elimination 4 must also be true. Although I initially thought it would not be. I know an anonymous inner class cannot have a constructor so that must be what it is refering to - passing variables to a constructor. On the other hand you can create an initialization block to initialize variables in the class itself. Maybe it's just me but the wording threw me on this one. If I'm wrong or off base someone fix me.
Dave
 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, Sathi:
I was having problems with the wording too.
If I was forced to guess, I would make the same choices.
However, I seem to be having lots of problems... you've noted
some. With respect to Choice #5, I guess an anonymous class could be (implicitly) static if it were included in a top-level nested class? Or would that disqualify it as an "inner class"?
I think this question needs to be clarified somehow.
 
Denis Anoykin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
About answer #5
What is syntax of anonymous static classes?
We can create instance of static inner class using full name of class only. I think this is cause why we can't create static inner class.
This is example of anonymous class definition syntax:
Runnable r = new Runnable () {
public void run() {
int p=0;
}
};
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4. Anonymous inner classes can never have initialization parameters.
What does "initialization parameters" mean? If it means "constructor parameters", then choice 4 is wrong.
Anonymous inner classes can use constructor which has parameter(s).
For example:
Frame f=new Frame();
f.add(new Button("test"){addActionListener(...)});
 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I interpreted this the same way as you, Bin.
I originally settled for #2 as the only correct answer, and only later noticed that you were supposed to pick *3* choices.
Maybe it was intended as a single correct choice question and there was a typo?
Ranjan
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct answers are
1. A non static inner class may have static members.
Keep in mind that static final fields ARE obviously static!

2. Anonymous inner classes cannot have any 'extends' or 'implements' clause.
That's the easiest one.
5. Anonymous inner classes cannot be static.
Even if you create an anonymous class in a static context, the class itself will not be static.
You may refer to :
15.9.5 Anonymous Class Declarations
An anonymous class is never abstract (�8.1.1.1). An anonymous class is always an inner class (�8.1.2); it is never static (�8.1.1, �8.5.2). An anonymous class is always implicitly final (�8.1.1.2).

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Anil:
Correct answers are
1. A non static inner class may have static members.
Keep in mind that static final fields ARE obviously static!
.................

Hi Paul,
Could you please give us an example program showing non-static inner class can have static final field(s)? I tried, but failed.

[This message has been edited by Bin Wang (edited June 11, 2001).]
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
class B
{
static final int VAL = 10;
}
}
 
Bin Wang
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Paul. I understand it now.
One more thing: Could you please explain what is the difference between static final int i=1; and final int i=1; from the compiling and executing point of view?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3764
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly same as the difference between static int i=1; and int i =1;
One is a static member and the other is an instance member!
'final' only makes them constant.
------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Denis Anoykin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
It is great hint about static final modifier
In context of this question maybe you can clear next moment:
Why we can't use not-final static members in non-static inner classes?
I know that it is declared in the lang spec. But why it is necessary? I can't find any cause of this fact. Only one that I can suppose that it is lang spec authors fancy.
Could you explain this moment more detailed (cause of this rule and example with Java code)?
 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,
That was a good question, though I still have problems interpreting one choice, #4.
#1 is great and mind-expanding.
With #5, I still have a problem. In my interpretation, JLS 8.1.1 doesn't say that anonymous classes cannot be static. And JLS 8.5.2 does not rule this out either, but just specifies the use of the "static" keyword.
In fact, the following code (adapted from Mughal, Sec 7.5) would seem to suggest otherwise:
--------------------------------------------

--------------------------------------------
Since no instance of Test was created, it must be that the anonymous "A"
returned in main() must be static. Otherwise an error about making a reference to a non-static variable would arise.
Am I missing something?
Thanks.
Ranjan
[This message has been edited by Ranjan Chaudhuri (edited June 12, 2001).]
 
Ranjan Chaudhuri
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me take a crack at answering my own question, since the JLS is never wrong.
It must be that what is being returned is a local final variable. That would compile too, and then everything is consistent.
This would suggest that Mughal Sec. 7.5 is inaccurate, and answer #5 is correct.
I will send a note to Mughal.
Thanks, Paul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic