Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static inner class

 
Ramnath krishnamurthi
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
How do i instantiate a static inner class?
The following code throws an exception
package innerclasses;
/**
* Title: Java Wanna Bee
* Description:
* Copyright: Copyright (c) 2004
* Company: Like Minds Consulting,Inc
* @author Ramnath Krishnamurthi
* @version 1.0
*/
public class StaticInner {
public StaticInner() {
}
static class myinner5{
public void methodA(){
}
}
public static void main(String[] args){
StaticInner test = new StaticInner();
StaticInner.myinner5 innertest = test.new myinner5();
//StaticInner.myinner5.methodA();
}
}
Exception:
"StaticInner.java": Error #: 479 : illegal qualifier; class innerclasses.StaticInner.myinner5 is not an inner class at line 22, column 47
"StaticInner.java": Error #: 308 : non-static method methodA() cannot be referenced from a static context at line 23, column 26
Any Help will be appreciated.
Thanks,
Ramnath
Feed an Opportunity.Starve a problem
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here i gave small example about creating instance for static inner class.
class StaticInner
{
static class Inner{
public void m(){
System.out.println("Inner class");
}
}
public static void main(String[] args)
{
new StaticInner.Inner().m();
}
}
 
atiqur rahman
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also do it like this:
class Outer {
static class Inner {
public void m() {}
}
}
public class TestClass {
public static void(String args[]) {
Outer.Inner in = new Outer.Inner();
//you do not need a outer class instance to instantiate a
//static inner class
in.m();
}
}
 
stephen narayan
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I m quite new to this stuff, hope this change may help u.
public class StaticInner {
public StaticInner()
{
}
static class myinner5{
public void methodA()
{
System.out.println("Inside Method 5");
}
}
public static void main(String[] args){
StaticInner test = new StaticInner(); //not needed!
StaticInner.myinner5 innertest = new StaticInner.myinner5();
innertest.methodA();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic