File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static inner class" Watch "static inner class" New topic
Author

static inner class

Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
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

Joined: Jul 28, 2003
Posts: 1844

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();
}
}


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
atiqur rahman
Greenhorn

Joined: Aug 30, 2003
Posts: 10
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

Joined: Apr 11, 2004
Posts: 1
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();
}
}
 
 
subject: static inner class