• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

What is Static Variable?...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Experts ,
Can any one say what is Static Variable,Static Method and Static Class...
Why a Static method cannot call another static method...?
 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why a Static method cannot call another static method...?



It looks like a static method can call another static method:



This compiles, runs and prints "Something".
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static metjods and variable belong to the class, and as such they can be called with out creating an instance (object) of the class.

As no object of the class exists a static method can not call any non static methods becuase it dosnt have access too them (there is no this available to a static method).

The classic example of static variables is for counting the instances of a given class. For example you might wish to know how many users are logged into the system, or how many requests are currently in a ssystem.

Another use of static variables is as constants (public final static) for a given class/object or API, although enum is perhaps better.

One use of static methods (along with static variables) is in creating Singletons.

non static methods, can call static methods.

How this helps
G
[ August 06, 2007: Message edited by: Gavin Tranter ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a couple of concepts...

I'm starting with instance variables just to set the stage...

An instance variable is defined by the class but it is owned by the object that is intantiated from the class. In other words, if you haven't instantiated one yet...... then it doesn't exist. That is why a static method can not see an instance variable or method.

A static variable is owned by the class itself and exists without the need for an instantiated object. Rather than a reference variable to an instance of the object, it's reference must be anchored by the class name itself.

Another point that I have not seen above is that, static methods/variables are shared by ALL INSTANCES of the CLASS that declares them. THAT is why they are often used as flags/counters across MULTIPLE objects. Because each individual object points to the same variable or method.

So, when a new object is instantiated, each instance variable is a new copy, unique to THAT particular instantiated object. each static variable is a single copy that each individually instantiated object points to... the very same one is shared.

A little difference in talking about methods..... this might seem a little confusing but just try to think it over a bit....

All methods are shared.... there are just two ways that we need to understand. (for the MOMENT, just for simplicity, I am omitting any discussion of threading/synchronization issues, just to make presentation a little clearer on the point.)

1) An instance method is a shared routine that is safe to share because each object that refers to it has it's own state, it's own variables. BUT when it is called... an instantiated object IS in CONTEXT. Meaning that there IS one associated with this method. Thus an instance variable must be anchored to some instance of an object (even if it is "this" object inferred as in .... this.methodName() ).

2) A static method is shared but there is no specific object associated with this invocation of the method so there are no instance variables/or instance methods visible. You cant see them from the static method... so you can't refer to them.

But a static can refer to static variables and static methods as allowed by access modifiers.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You asked what a static class is, but that doesn't exist. Only variables, methods and blocks can be static.

regards,

Carl
 
author and iconoclast
Posts: 24206
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carl Wauters:

You asked what a static class is, but that doesn't exist. Only variables, methods and blocks can be static.



Nested classes can be static.
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic