Results 1 to 3 of 3

Thread: When to Declare a static method in JAVA

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

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

  3. #3
    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; 
          ) 
      )

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Static method in Abstract class
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:22 AM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  4. Inheritance and polymorphism in static method
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 11:46 AM
  5. What if we declare the main method as private?
    By Baran in forum Software Development
    Replies: 4
    Last Post: 25-02-2009, 07:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,968,746.31623 seconds with 17 queries