Java Programming - Inner Classes - Discussion
Discussion Forum : Inner Classes - General Questions (Q.No. 3)
3.
Which is true about a method-local inner class?
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 1 of 2.
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();
}
}
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)
Niks said:
1 decade ago
Why inner class can't be made public ?
Shreeja said:
1 decade ago
Because it is only visible to the enclosing class so no use making it public.
Moin khan said:
1 decade ago
Why inner class cannot be made static?
Smartgoat said:
1 decade ago
Why a method-local inner class does not have to be declared final?
Karishma said:
1 decade ago
Why method is not static?
Harish said:
1 decade ago
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.
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:
1 decade ago
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?
Don't you think this question contradicts the explanation given here about inner class variables made public?
Anamica said:
1 decade ago
It can be marked as static but it can only be accessed by the outer class.
Shweta said:
1 decade ago
Please explain clearly why option B is correct?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers