• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class problem

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In below code

EnclosedClass.InsideClass insideObj1 = enclosedObj1.new InsideClass("Ganish");



= enclosedObj1.new InsideClass("Ganish");

I got this code. InsideClass is also an instance variable of EnclosedClass so an object of this class can create an object of InsideClass which is it's member. Hope I'm right ?

But what does below code means? How could I access a class by using enclosedclassname.innerclassname to create an object of inner class in outside class ?

EnclosedClass.InsideClass insideObj1


 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by changing the names of the classes to Outer and Inner. EnclosedClass is very misleading.
Get rid of the capital letters in every identifier which isn't a class name.
Then at least you will be able to see what is happening.
Your comment at the beginning is very misleading. You are not creating an inner class instance inside the outer class. You are creating it inside an object. For inner classes (and you are right to call that an inner class) you need an enclosing object and the syntax is
objectName.new InnerClassName(...)
Which you have used, and I presume your code ran normally
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Campbell Actually I did more two programs before, named Outer, Inner where I declared & initialized Inner class object in Outer class( In declaration ) and used that object to access instance variables and members of Inner class. In second program I created an object of Inner class( named InnerClass ) in main method of Outer(named OuterClass) class and called its members so already had used those names so just to differ them from each other named like that. But yes I got your point at least here I should name them Outer & Inner.

In Identifier and method naming convention first words in small letter and if second word comes then first letter of that word in capital.
Ex. method = void displayResult();

an identifier int maleAge(); is this correct ? I think I read it in Java Programming Style Guide.

yes your right, it not creating an object of inner class in outside class But creating it inside an object. yes it ran successfully.

I'll make changes in above code a you guided. Thank you.

 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opps ! I think i can't change it now. anyway
 
reply
    Bookmark Topic Watch Topic
  • New Topic