File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Which way is efficient, declaring a function as static or creating an object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which way is efficient, declaring a function as static or creating an object?" Watch "Which way is efficient, declaring a function as static or creating an object?" New topic
Author

Which way is efficient, declaring a function as static or creating an object?

krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi everybody, I am Chaitanya, I have a simple class which will call a function from main() method. Since we cannot access non-static methods from static context: should I declare the called function as static or should I create an object of the class in which main() is declared.

Which way is efficient? Thank you all in advance. Have a nice day.


There is no rule that we all should know everything. Lets learn few things of everything here.
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

I have a simple class which will call a function from main() method. Since we cannot access non-static methods from static context: should I declare the called function as static or should I create an object of the class in which main() is declared.

Which way is efficient?


I can say.. static and non-static have different meaning. So first check the method which you want to call. If the method looks like same for all object of the class then its a static method. That means a method having static characteristic should be declared as static and not any/all method just for the ease of calling it.

I hope you get my idea.


Dhan
SCJP - Here for Knowledge..
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Hi Dhan, thanks for the reply, I do know the use of static, but I just want to know which way is efficient. Consider an example shown here.

In this example I created an object to the PrimeNumber class and accessed the valid(String) method. However I can access it by declaring it as static. In this particular case which way is efficient?

Thank you in advance.
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

For (and only for) your given example...I see method public boolean valid(String s) is kind of a utility method . It takes an input String and checks whether it consists of only numeric digits. So It has nothing to do with the state of the Object.....hence this method should be defined as static.

Way to go :-)
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
Thank you Dhan. Can you explain what is this.
So It has nothing to do with the state of the Object
Dhan Kumar
Greenhorn

Joined: Aug 03, 2009
Posts: 29

So It has nothing to do with the state of the Object :------- The method(it) does not change the state of the Object that means it does not play with the instance field or call any other method which may do that. For all object on the same String parameter the method will return the same value.

Is it clear to you now..I hope so.
Anyway thanks for quick responses.
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
I got it now, thank you Dhan. Infact I have to thank you Pal, for your quick replies.
Colin Wright
Greenhorn

Joined: Apr 21, 2010
Posts: 8
You should also declare your variable "a" inside the method if you make it static or at least make that a static property of the class otherwise I do not think it will work.

At the moment a is declared outside the valid method and isn't static so couldn't be used inside valid if you made that method static.

At least that's what I understand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which way is efficient, declaring a function as static or creating an object?
 
Similar Threads
designing categories and adverts
When to extend and when to create object
Creating classes
Why main is declared as static?
Rgarding datatypes