• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Static class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is a static inner class. When do we use it.
 
Greenhorn
Posts: 18
  • 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
 
Greenhorn
Posts: 1
  • 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
  • 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
 
Ranch Hand
Posts: 33
  • 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).]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!