Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Accessing inner class from method

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain me how can I create an instance of the class Inner ?
class ClassInsideMethod{
public void aMethod(){
class Inner{
public Inner(){
System.out.println("Inner class constructor invoked");
}
}//end of inner class
}//end of method
public static void main (String args []){
ClassInsideMethod m = new ClassInsideMethod();
}//end of main
}//end of class
What is the default access specifier for constructor ?
Thanks in advance.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Salim, the Inner class define inside method, so just add the instance stmt at the inner class.
 
Salim Mohamed
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kevin. What if the inner class is defined static (i.e. nested). How can I inovke ?
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, the Inner class define inside method is private to the method and cannot be marked with any access modifier, neither can they be marked as static. It will get a compiler error. If the Inner class outside the method, you can invoke like this.

A static inner class does not have any reference to an enclosing instance. Because of this, methods of a static inner class cannot use the keyword this(either implied or explicit) to access instance variables of the enclosing class. Thsi is just the same as the rules that apply to static methods in ordinary classes. You can create an instance of static inner class without the need for a current instance of the enclosing class.
That is all
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic