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

Instantiating inner class

 
Latha Kota
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can anyone tell me if I can instantiate i.e declare an object of inner class with in the main method. For example:
class Myouter{
int x =7;
public static void main(String []aa){
Myouter.Myinner in = new Myouter.Myinner();
in.inner();
}
class Myinner{
public void inner(){
System.out.println(x);
}
Is this code correct? Thanks in advance.
Latha
 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because Myinner is not static then you have to instantiate both Mouter and Myinner.
Myouter.Myinner in = new Myouter().Myinner();
[ July 05, 2003: Message edited by: Syamsul Hussin ]
 
Alex Radomski
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From any static code:
Outer.Inner in = new Outer().new Inner();
 
Latha Kota
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Alex and Syamsul. This will help..
Latha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic