Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using static class

 
ricky gonzalez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I wonder how we use static classes. Do we instantiate them? What exactly are they differing from regular classes?
Thanks!
 
zulfiqar raza
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean Static Member classes? also known as Top-Level Nested Classes.
These classes can be used as any regular Top level classes. They can be instantiated without an outer instance associated with them and can also define both static and non-static members, like any top level package member. However they can also access Static members from the enclosing class.
Zulfiqar Raza
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A free-standing top level class cannot be static. A static class can exist only within the context of another class. Static InnerClasses are not associated with an instance of outer ( enclosing ) class.

Usually static member classes( aka nested class ) are used as helper classes for the enclosing class.
Hope that helps!
Ajith

[This message has been edited by Ajith Kallambella (edited November 08, 2000).]
 
Subha Guhan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajith
I have some doubts if the methods are IMPLICITLY STATIC.
I read in Khalid Nested Top level classes can have both static
and non static methods.
I tried it out

Correct me if I am wrong.
Subha
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right and my wordings were confusing
I have edited my post to make it clearer
Thanks!
Ajith
 
SHALINI PATEL
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subha,
I have a doubt regarding your example,
Here yu r calling a non static method in the main which is static, Is this valid? I mean by calling it even through object reference i.e o1.nonstaticmethod().......... I cant understand this, can yu explain it more clearly... Hope I dont sound so silly.

SHALINI
 
Sasikanth Malladi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini, a static method can access member variables or methods only thru a reference to an object, so this is perfectly legal and makes sense as well.
HTH
Shashi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic