Java Programming - Inner Classes - Discussion

Discussion Forum : Inner Classes - General Questions (Q.No. 3)
3.
Which is true about a method-local inner class?
It must be marked final.
It can be marked abstract.
It can be marked public.
It can be marked static.
Answer: Option
Explanation:

Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful).

Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so).

C and D are incorrect because a method-local inner class cannot be made public (remember-you cannot mark any local variables as public), or static.

Discussion:
15 comments Page 2 of 2.

Thirumal said:   1 decade ago
I think Option B is wrong. Can You explain with an example?

Mikhail said:   1 decade ago
Inner class can be abstract, final, private, protected.

Why only b is correct ?

Bisan said:   1 decade ago
Option B is the right answer as per the options available method-local inner class can be marked abstract or final.

Option A is wrong just because it said must in the statement as it is not mandatory to mark final.

Sasi said:   8 years ago
Static block, static variables and methods are called before object creation in order to initialize static data members, constructors are used to initializing instance data members. So before object creation, static block called. For inner classes no need of making static. That's is a reason for option D incorrect.

Loc Tran said:   8 years ago
Here is the example proving Option C is correct!

class Demo {
private int data=30;//instance variable
void display(){
final int value=50;//local variable must be final till jdk 1.7 only
class Local{
public void msg() {System.out.println(value);};
}
Local l= new Local();
l.msg();
}
public static void main(String args[]){
Demo obj=new Demo();
obj.display();
}
}
(2)


Post your comments here:

Your comments will be displayed after verification.