• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
krishna Karthikk
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Dhan. Can you explain what is this.
So It has nothing to do with the state of the Object
 
Dhan Kumar
Greenhorn
Posts: 29
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it now, thank you Dhan. Infact I have to thank you Pal, for your quick replies.
 
Colin Wright
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic