• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static problem !!

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
Can anyone throw some light on the difference
between a statically initialized code and a
static function ? Also what advantage does a
static function have ?
Thanks & Regards,
Rajesh .V
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if by function you mean member then you are adding to the class's interface so the method can be called and something will happen. this can happen at anytime that the class is loaded by the JVM. static blocks are run once and once only when the class is loaded and are more to do with the internal needs of static members of the class - they are not externally "referencable" in the way a static method is. so for e.g. a static init block might load a properties file of constants, and a static method would provide access to those constants to other classes.
i am only a greenhorn - but that is how i read it
[ October 03, 2002: Message edited by: peter greaves ]
 
Rajesh Venkatesh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a function is defined to be static (and a
memeber of a class) it does get called automatically when the class is loaded ! We need to make an explicit call to it. This may as well
be achieved by marking it non-static ..whats
the advantage ?

Thanks & Regards,
Rajesh .V
 
peter greaves
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"If a function is defined to be static (and a
memeber of a class) it does get called automatically when the class is loaded "
not for me. this prints "Static init block" and "Static method" follows only _if_ //1 is uncommented:


maybe i am misunderstanding your issue?
 
Rajesh Venkatesh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
My question is static function needs to be called
explicitly just like a non-static function..then
whats the use of keyword static if given along with the function ?
Thanks & Regards,
Rajesh .V
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajesh,
you may want to have a look at the following section of the Java tutorial:
Understanding Instance and Class Members
I might be moving this discussion to Java in General (Beginner) soon...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic