Get the tools you need to learn Java skills fast!
Video tutorials, eBooks, hands-on lab exercises, sample code.
Get started
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Instantiating inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Instantiating inner class" Watch "Instantiating inner class" New topic

Instantiating inner class

Latha Kota
Ranch Hand

Joined: Mar 13, 2003
Posts: 35
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();
class Myinner{
public void inner(){
Is this code correct? Thanks in advance.
Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
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 ]

----------------------------------<br />SCJP 1.4
Alex Radomski

Joined: Apr 24, 2003
Posts: 24
From any static code:
Outer.Inner in = new Outer().new Inner();
Latha Kota
Ranch Hand

Joined: Mar 13, 2003
Posts: 35
Thanks Alex and Syamsul. This will help..
I agree. Here's the link:
subject: Instantiating inner class