• 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

Difference between static and private static method

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Please tell me the difference between static and private static method and where are they used.

Thanks in advance
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijaylaxmi Agarwal:
Hi all,

Please tell me the difference between static and private static method and where are they used.

Thanks in advance



Hello and welcome to JavaRanch.

Static modifier is used to create variables and methods that will exist independently of any instance created for the class. Static members exists before any instance of the class is created.
Also there will be only one copy of the static member.

To call a static method displayRuns() of the class named Cricket we write

Cricket.displayRuns();

Class name is used to invoke the static method as static member does not depend on any instance of the class.

private static method means you can not invoke the method from outside the class as the method is private to the class.

Hope you get the explanation.

 
sudipto shekhar
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
think of private static methods as the membeers that belong to the class and not of any instance of the class.

Static member of the class are called even before the object of that class are created.

Like the most common example, you want to know how many of the instance of the Dog class is created. To find out this we write the code as follows.

Here in the example the private member is the count variable which is the static member. A private static method is loaded in the memory even before the instance of the class is created.
And since the member is private it can not be called from outside the class.

 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic