• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how to understand static method

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,I have a question.Please consider the following example
public class A
{
public void methoda()
{...}
}
public class A1
{
public static void methoda1()
{...}
}
Then I try to triger those methods
public class B
{
//approach 1
A a=new A();
a.methoda();
//approach 2
A1.methoda1();
}
what I canot understand is what is the difference between approach1 and approach2.(suppose methoda() and methoda1() do the same thing)And under which condition I should use approach2.
Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, here's one example: If you're creating a class of "utility" methods (like java.lang.Math), then you might want to make the methods static. This way, you can use those methods without ever creating an object of that class. (Note that there's no need to ever create an instance of Math. In fact, you can't.)
[ September 13, 2004: Message edited by: marc weber ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think this way. Static methods are class methods. When you say 'class' methods, the method is associated with the class(blue print itself) rather than instances(copies). So, irrespective of the no. of objects you create(copies you make from class) on the class, this 'static' method is one and only one, since it is static.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or see it this way:
If your method doesn't use a membervariable, you can make it static.
And then you don't need an instance of the class, to use the method.
 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for ypur explanation. This time I can understand.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to all the info provided by the people above, it is more efficient to use a static method than invoke a method via an object. There is overhead to create objects.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yan,

In normal development, you can not decide to declare a method static, for performance-reasons. That would lead to the opposite of design.
 
A feeble attempt to tell you about our stuff that makes us money
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic