Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Inner Class

 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone
public class TodayDate
{
public static void main(String s[])
{

int test=10;
class Second // Inner Class with in main function
{

void fun1(){
System.out.println(test); // accessing the local variable 'test'
}
}
}
}

In the above example while i am accessing the 'test' local variable then the program gives compile error.

Can anyone give the logic behind it.

Thanks in advance.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method-local class may be defined in a static method. In this case, the instance of method-local inner class can only access the static members of the enclosing class.
 
Vladimir Scheglov
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
local variable "test" must be final
 
Sujittt Tripathyrr
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That i know but i want the proper suggestions.

Thanks for the reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic