• 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 Methods?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm kinda having some problem with static methods ....

Can anyone tell me what's the use of static methods ???

I know that when you declare a method as static you cannot refer to instance variable

A static method is always called using it's classname, still you can call static method using it's object too.

I'm really not getting this.....

Thanks for reading it
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without using static method we can not run any j2se program.
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anbarasu aladiyan wrote:Without using static method we can not run any j2se program.



But what's the main use of using a Static method....that's my question.
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But what's the main use of using a Static method....that's my question.


its a big topic. Tell me why we need to declare main method as static type ??
If you know the answer fo the above question, that is the answer for your question too.
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We declare main method as static just to make sure that..there's only one main method in a specific class...

anbarasu aladiyan wrote:

But what's the main use of using a Static method....that's my question.


its a big topic. Tell me why we need to declare main method as static type ??
If you know the answer fo the above question, that is the answer for your question too.

 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess i got it...
A method should be declared as static if :

1. It has no reference to a instance variable.
2. If we want to make sure that the method shouldn't be overridden.
Then we can define the method as a static method....
If i'm wrong then please correct me...or if there's any other point which should be mentioned then please let me know.
Thanks for your replies..

Vasiq Molvizadah wrote:We declare main method as static just to make sure that..there's only one main method in a specific class...

anbarasu aladiyan wrote:

But what's the main use of using a Static method....that's my question.


its a big topic. Tell me why we need to declare main method as static type ??
If you know the answer fo the above question, that is the answer for your question too.

 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We declare main method as static just to make sure that..there's only one main method in a specific class...


No. we can have overloaded versions also. But main method must be static. Because static scope is the higher level of scope in java.

They are loaded when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.

So it means it is useful to do any work that is not specifically tied to any object.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to create a method which is not associated with instance fields of the class, it can be declared static. Lets take the java.lang.Math class for example. It has a lot of mathematical methods which just do some calculations on the parameters passed, so they are all static so that you can directly call Math.sin or Math.sqrt. If the methods were not static, then we would need to uselessly create an instance of Math class to call these methods. The reason the instances of Math class would be useless is because the methods (sin or sqrt etc) don't need any instance variables. So you would find yourself doing this - new Math().sqrt(25) which would increase the memory usage of the program as well as create more objects for garbage collector...
 
Vasiq Molvizadah
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok i get it...thanks a lot for the explanation dude...


Ankit Garg wrote:If you want to create a method which is not associated with instance fields of the class, it can be declared static. Lets take the java.lang.Math class for example. It has a lot of mathematical methods which just do some calculations on the parameters passed, so they are all static so that you can directly call Math.sin or Math.sqrt. If the methods were not static, then we would need to uselessly create an instance of Math class to call these methods. The reason the instances of Math class would be useless is because the methods (sin or sqrt etc) don't need any instance variables. So you would find yourself doing this - new Math().sqrt(25) which would increase the memory usage of the program as well as create more objects for garbage collector...

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic