Results 1 to 6 of 6

Thread: What is static method in java?

  1. #1
    Join Date
    Jan 2009
    Posts
    10

    What is static method in java?

    Hi,

    Anyone explain me what is static method in Java?

    Thanks

  2. #2
    Join Date
    May 2008
    Posts
    44

    Re: What is static method in java?

    There are two types of methods.

    • Instance methods are associated with an object and use the instance variables of that object. This is the default.
    • Static methods use no instance variables of any object of the class they are defined in. If you define a method to be static, you will be given a rude message by the compiler if you try to access any instance variables. You can access static variables, but except for constants, this is unusual. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class.


    Calling static methods

    There are two cases.

    Called from within the same class

    Just write the static method name. Eg,

    Code:
    // Called from inside the MyUtils class
    double avgAtt = mean(attendance);
    Called from outside the class
    If a method (static or instance) is called from another class, something must be given before the method name to specify the class where the method is defined. For instance methods, this is the object that the method will access. For static methods, the class name should be specified. Eg,

    Code:
    // Called from outside the MyUtils class.
    double avgAtt = MyUtils.mean(attendance);
    If an object is specified before it, the object value will be ignored and the the class of the object will be used.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: What is static method in java?

    static variables are classes variables not instance variables .They are instantianted only once for a class.They are initialised at class load time.
    Static method can be referenced with the name of the name of the particular object of that class. That's how the library methods like System.out.println works.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: What is static method in java?

    Methods declared with the keyword static as modifier are called static methods or class methods. They are so called because they affect a class as a whole, not a particular instance of the class. Static methods are always invoked without reference to a particular instance of a class.

    The use of a static method suffers from the following restrictions:

    1. A static method can only call other static methods.

    2. A static method must only access static data.

    3. A static method cannot reference to the current object using keywords super or this.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What is static method in java?

    Static Methods:
    a. In a file context, it means that the function can be called only within the file scope.
    b. In a class context, it means that you can invoke a method of a class without having to instantiate an object. However note that in static methods, you can only manipulate variables that are declared as static, and not other variables.

    Code:
    static means that it cannot be changed

  6. #6
    Join Date
    Sep 2011
    Posts
    1

    Re: What is static method in java?

    static method can be referanced using super or this

Similar Threads

  1. Static method in Abstract class
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:22 AM
  2. Junit - mock static method
    By Parvati in forum Software Development
    Replies: 5
    Last Post: 11-08-2010, 05:06 PM
  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. When to Declare a static method in JAVA
    By Aamin in forum Software Development
    Replies: 2
    Last Post: 04-02-2009, 07:25 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,718,439,979.10567 seconds with 17 queries