Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

possible to instantiate Inner Class by itself

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether it is possible to instantiate Inner Class by itself! If not, I have got the below coding in this forum. How they instantiate Inner Class like this myAA my= new myAA();

class AA
{
int x=123;

public int getMyVal()
{
return x;
}
}

public class Test10
{
public AA getRef()
{
final int x=90;
class myAA extends AA
{
public int getMyVal()
{
return x;
}
}
myAA my= new myAA();
return my;
}

public static void main (String[] args)
{
Test10 t =new Test10();
AA a=t.getRef();
System.out.println("Value ---> "+a.getMyVal());
}
}
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myAA is a local class inside a method. Its type is not available outside of the method, you may have noticed that the getRef() method has a return type of the parent class AA. The instantiation is done inside the method, that is fine.
[ June 24, 2005: Message edited by: soumya ravindranath ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic