File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static class

 
Srinivas99
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a static inner class. When do we use it.
 
swapnil dey
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi srinivas
first bout an inner class, it is a class defined within another class. now to create instances of the inner class u need an instance of its containing class.. i.e given a class Outer that contains another class Inner, to create an instance of Inner u must use
a)Outer o = new Outer();
Inner in = o.new Inner();
or
b)Outer.Inner in = new Outer().new Inner();
but a static inner class doesn't need an outer class instance, the following code would work for a static inner class, but not for one which isn't static
Outer.Inner in= new Outer.Inner();
but as far as the usage is concerned, its something even i need help on....
swapnil dey
 
Alex Fon
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example of my usage:
I have class Errors for all error numbers definitions. All numbers are defined as static members to have access without creating class instance.
Now I want some errors hierarchy: for example separate Login and NewUser errors:
//---------------------------------------------------
public class Errors
{
public static final int UNEXPECTED = 0;

public class Login
{
public static final int PASSWORD_DOESNT_MATCH = 1;
public static final int USER_DISABLED = 2;
public static final int USERSELF_DISABLED = 3;
public Login(){};
}
public static Login jspLogin;

public class NewUser
{
public static final int SOME_FIELDS_MISSED = 1;
public static final int CANNOT_SAVE_TO_DB = 2;
public NewUser(){};
}
public static NewUser jspNewUser;
}
//------------------------------------------------
example of usage:
if (myError == Errors.jspLogin.PASSWORD_DOESNT_MATCH)
{
..........
}
 
swapnil dey
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi alex
can't we do the same thing by extending the classes Login & NewUser from the class Error.
any specific reason why inner classes be given preference over inheritance
swapnil dey
 
Rob Whelan
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static inner classes are also called "nested" top-level classes, because, they're basically the exact same thing as a top-level class.
The only reason for using one is organizational, as far as I know. For instance, you might have a class that doesn't need the usual inner class privileges, but will only ever be used by the class containing it.
Correction: I forgot that static inner classes have access to static members of the enclosing class -- including private static members. Also, the enclosing class has the same access into the inner class. More details here.
[This message has been edited by Rob Whelan (edited November 02, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic