Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 04-02-2009
Member
 
Join Date: Feb 2009
Posts: 84
When to Declare a static method in JAVA

Hello, I have a little but the confusing doubt that when we must declare a method as static?

Declare a variable to be static when the class or who is not a variable that identifies characteristics of individual items but a characteristic of the whole class ... who know, for example, every time I create a new object, the manufacturer of a variable increases class "counts" that keeps me on behalf of those objects have been created ....

But a method when I need to declare it as static?

Thanks
Reply With Quote
  #2  
Old 04-02-2009
Member
 
Join Date: Mar 2008
Posts: 227
Re: When to Declare a static method in JAVA

The cases / reasons may be different, but usually when the method does not act on a particular instance, or you do not need / do not want to create an in / out on call.
For example, if you're writing a class of utility (eg a container trivial generic functions that operate on strings of transformations), in relation to the type of application you write, you can not have the need to instantiate the class to call the methods, here in that case you can return a type of static, the contents of which are divorced from the context of the single instance of the class.
Reply With Quote
  #3  
Old 04-02-2009
Member
 
Join Date: Mar 2008
Posts: 192
Re: When to Declare a static method in JAVA

In more practical terms, if the method do () is not static (and therefore must use the variables you initialize with the manufacturer, etc..) Is used as follows:

Code:
 BohClass bc = new (...); BohClass 
  bc.do ();
If it is a static method (and hence should not use class variables) is used as follows:

Code:
 BohClass.do ();
A static method do:

Code:
 public class BohClass ( 
      pubic static int do (int n) ( 
          int x = 3; 
          return x + n; 
      ) 
  )
static method do not:

Code:
 public class BohClass ( 
      int y = 5; 
      public int do (int n) ( 
          int x = 3; 
          return y + x + n; 
      ) 
  )
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "When to Declare a static method in JAVA"
Thread Thread Starter Forum Replies Last Post
What is static method in java? Vaibhav S Software Development 5 25-09-2011 09:40 AM
Static method in Abstract class Anthony12 Software Development 6 12-08-2010 10:22 AM
Why main method is static in java? MKAIF Software Development 5 06-03-2010 04:20 PM
Inheritance and polymorphism in static method Vodka Software Development 5 04-03-2010 11:46 AM
What if we declare the main method as private? Baran Software Development 4 25-02-2009 07:35 PM


All times are GMT +5.5. The time now is 11:44 PM.