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)
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.
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.
Option A is wrong just because it said must in the statement as it is not mandatory to mark final.
Mikhail said:
1 decade ago
Inner class can be abstract, final, private, protected.
Why only b is correct ?
Why only b is correct ?
Thirumal said:
1 decade ago
I think Option B is wrong. Can You explain with an example?
Rakesh said:
1 decade ago
What is method -local inner classes?
Shweta said:
1 decade ago
Please explain clearly why option B is correct?
Anamica said:
1 decade ago
It can be marked as static but it can only be accessed by the outer class.
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?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers