File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Methods?" Watch "Static Methods?" New topic
Author

Static Methods?

Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
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


Human Knowledge Belongs to the World.
- Vasiq Mz
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Without using static method we can not run any j2se program.


A.A.Anbarasu
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
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

Joined: Jun 02, 2009
Posts: 182

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

Joined: Dec 24, 2009
Posts: 66
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

Joined: Dec 24, 2009
Posts: 66
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

Joined: Jun 02, 2009
Posts: 182

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.

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Methods?
 
Similar Threads
Confusion in extending Thread Class
New to Java and need help
Overloading static methods
does static methods inherit ? what about private ?
Static methods and instance variables are not overridden