• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class problem  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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

 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opps ! I think i can't change it now. anyway
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!