Java Programming - Inner Classes - Discussion

Discussion :: Inner Classes - General Questions (Q.No.3)

3. 

Which is true about a method-local inner class?

[A]. It must be marked final.
[B]. It can be marked abstract.
[C]. It can be marked public.
[D]. It can be marked static.

Answer: Option B

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.


Niks said: (Jul 9, 2012)  
Why inner class can't be made public ?

Shreeja said: (Jul 17, 2012)  
Because it is only visible to the enclosing class so no use making it public.

Smartgoat said: (Sep 11, 2013)  
Why a method-local inner class does not have to be declared final?

Moin Khan said: (Sep 13, 2013)  
Why inner class cannot be made static?

Karishma said: (Sep 22, 2013)  
Why method is not static?

Harish said: (Nov 9, 2013)  
Initially,

Static variables.
Static method.
Static block.

Are loaded before object creation and they are not part of the object.

So, for inner method we cannot declare static because static method should be loaded before object creation, by using object reference we are calling the method so, we cannot use inner method as static.

Manu said: (Nov 16, 2013)  
http://indiabix.com/java-programming/inner-classes/016001.

Don't you think this question contradicts the explanation given here about inner class variables made public?

Anamica said: (Jan 6, 2014)  
It can be marked as static but it can only be accessed by the outer class.

Shweta said: (Jan 24, 2014)  
Please explain clearly why option B is correct?

Rakesh said: (Apr 27, 2014)  
What is method -local inner classes?

Thirumal said: (Jul 3, 2014)  
I think Option B is wrong. Can You explain with an example?

Mikhail said: (Sep 1, 2014)  
Inner class can be abstract, final, private, protected.

Why only b is correct ?

Bisan said: (Nov 11, 2014)  
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: (Jun 30, 2017)  
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: (Jul 9, 2017)  
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();
}
}

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.