Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default purpose of a static methods

    i am taking a class on java and learning about static methods and i am confused on what makes a static method different from an instance method. And what is the purpose of a static method?

    any help would be appreciated

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A static method doesn't require you to have an instance of the class. If you for example create a method to add 5 to some object, you would need to first create an object of the class, then apply the method to an object.
    Java Code:
    class Example{
      int x;
      public void add(Example e){
        x + e.x;
      }
      public static void main(String[] args){
        Example ex = new Example();
        Example ex1 = new Example();
        ex.x = 5;
        ex1.x = 10;
        ex.add(ex1);
        System.out.println(ex.x);
      }
    }
    In a static method you don't need to have an instance of the required class, there are many helpful static classes in math, for instance, you can apply Math.sqrt() to any item because it's static.

    A lot of times when I use static methods, they are used as "helper" methods.
    Java Code:
    public class Example2{
      public static boolean isPrime(int n){
        int x = Math.sqrt(n);
        boolean flag = true;
        for(int i = 2; i< x; i++){
          if(n % i == 0){
            flag = false;
            break;
          }
        }
        return flag;
      }
      public static void main(String[] args){
        System.out.println("Please enter a number: ");
        int y;
        Scanner scan = new Scanner(System.in);
        y = scan.nextInt();
        ArrayList<Integer> primes = new ArrayList<Integer>();
        for(int i = 1; i <= y; i++){
          if(isPrime(i)){
            primes.add(i);
          }
        }
        for(Integer num : primes){
          System.out.print(num + " ");
        }
      }
    }
    This uses a static method to test if a number is prime, in this examples, you do not need to create an instance of the class Example2.
    Last edited by sunde887; 02-10-2011 at 02:16 AM.

  3. #3
    akramwahid is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Normal if u want to access a method from another class what do u need to do? U have to create an object of that particular class and access the object.
    for example u have 2 classes called class1 and class 2, from ur class 2 u want access a method called getCalculate() from class 1, so u have to create an object of class 1 like this
    Class 1 ppp=new class1();
    and to access that method
    ppp.getCalculate();

    What static method do is,
    u can directly called the method like this, class1.getCalculate()
    u no need to create an object of that class which contain the static method. Also the advantag is it reduce the memory wastage in ur PC..
    Last edited by akramwahid; 02-10-2011 at 02:21 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by akramwahid View Post
    What static method do is,
    u can directly called the method like this, class1.getCalculate()
    u no need to create an object of that class which contain the static method. Also the advantag is it reduce the memory wastage in ur PC..
    Please elaborate on how it causes a significant reduction in memory "wastage". Are you saying that we should use nothing but static in order to conserve memory, that OOP programming results in nothing but bloat? And how much memory are we saving if I do this in a typical program?

  5. #5
    akramwahid is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Its ur convenient using static method, otherwise u have to create an object and call the method. Also why i said reducing memory wastage is, if we creating an object means, a separate memory allocation is done by the computer , is it? and ur application going to be with good performance.... Always programming means giving solution for a problem. Good programming means giving solution in an easiest ways, so its ur convenient .....

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by hoosierfan24 View Post
    purpose of a static methods
    To give your friends a zap after walking across a carpet.

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    @akramwahid:
    If you find OOP to be bloated and inefficient, what are you doing on a Java board?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by m00nchile View Post
    @akramwahid:
    If you find OOP to be bloated and inefficient, what are you doing on a Java board?
    Expanding, slowly.

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Heh, and i gus wrting liek dis savs memry 2 :D
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by masijade View Post
    Expanding, slowly.
    Of course, because if the OP had expanded instantaneously we'd call it an explosion; yech, all those bloody remnants all over the place ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by akramwahid View Post
    Its ur convenient using static method, otherwise u have to create an object and call the method. Also why i said reducing memory wastage is, if we creating an object means, a separate memory allocation is done by the computer , is it? and ur application going to be with good performance.... Always programming means giving solution for a problem. Good programming means giving solution in an easiest ways, so its ur convenient .....
    My questions were somewhat rhetorical. While you may be somewhat technically correct, that using OOPs techniques adds some overhead to a program's size, and possible speed, and even this is minimized by today's optimizing compilers, the question is does it add a practical overhead, one that is sensed by the user or developer, and the answer in over 99.999% of the time is no, it does not and so your reasons for using statics are not valid.

    The other question to ask is what is the rate-limiting-step in software development? Is it code or memory bloat, or is it the difficulty in writing logically organized and readable code that has minimal bugs and that can be extended or enhanced with the least amount of difficulty. The answer in my opinion is the latter, and it is here that using good OOPs techniques can help immensely. YMMV.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    Is it code or memory bloat
    It is a bit of a pity that the JVM specification (and the .class format specification) is a bit technical and not frequently read by people new to programming). Sometimes people even think that objects carry around their own (non static) methods (this is not so). The Java class format is quite a clever one and the vft (Virtual Function Table) format can be initialized (and used) in quite a fast way. There is (almost?) no code bloat in Java, it just offers a better organization of code (and data), that's all. People who are afraid to create another method or another class shouldn't be, there's (almost) no overhead and it adds to the clarity of the code and data big times. Granted, every ease of code generation and data organization comes with a bit of overhead but we don't want to program in assembly language anymore.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Sometimes people even think that objects carry around their own (non static) methods (this is not so).
    I worked with a chap that thought that.
    He'd written a data class that was to be RMId from server to client and stripped all the getters and setters out because that would save space when serialising, and reduce the size of any collections.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    I worked with a chap that thought that.
    He'd written a data class that was to be RMId from server to client and stripped all the getters and setters out because that would save space when serialising, and reduce the size of any collections.
    Here's my personal favourite (less than two weeks old) "The Spring framework is terribly inefficient because it uses overly long names" ... uttered by some twit who claims to know what he's talking about ... I think Edsger W. Dijkstra was right: a touch of Basic ruins your mind for life ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Instant respect killer!

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by masijade View Post
    Instant respect killer!
    Oh well, I might as well post my Coding Guide Lines (c) (tm)

    1) everything should be public; there's no reason to keep things a secret to other programmers or customers.

    2) everything should be static; if everything keeps moving around all the time they're hard to find.

    3) variable names, method names and class names should be kept as short as possible. Disk space is expensive and memory is precious; we don't want to waste any of it, do we?

    4) interfaces are an invention of the devil: they're slow and obfuscate the code; don't use them.

    5) booleans are worthless; we should use 1 and 0 for true or false; or vice versa when it's more convenient for your code but you should always document it clearly in a separate file.

    6) use literal constants as much as possible; symbolic constants are a waste of namespace (also see point 3).

    7) preferably integer variable names should start with one of the letters in the range i ...n; all other letters indicate a double type.

    7) don't use CamelCase; it makes the reader dizzy. Use all lowercase names and prepend them with a mnemonic indicating their type (also see point 7).

    8) uppercase names are allowed when you want to shout to the reader; e.g. when something is very important and the reader should pay special attention to it, capitalize.

    I hope that this short list guides everyone, who's willing, to clever, fast and efficient code.

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Words? You use words? I write in base64 to keep the code small and concise.

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by quad64bit View Post
    Words? You use words? I write in base64 to keep the code small and concise.
    Luxury! I normally whistle the bits in my old audio coupler (it has been defect for years).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You have an audio coupler? That must be nice. I used to just wrap my knuckles against the mercury delay lines in the right sequence to -hard code- my ram. That is, when I wasn't pressing the bits into clay tablets with a reed on the banks of the river near my straw hut. :D

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,769
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by quad64bit View Post
    You have an audio coupler? That must be nice. I used to just wrap my knuckles against the mercury delay lines in the right sequence to -hard code- my ram. That is, when I wasn't pressing the bits into clay tablets with a reed on the banks of the river near my straw hut. :D
    Well, I call it an audio coupler; actually it's the head of one of my dead sibblings; I yell into his ear for 29 hours per day but he doesn't remember one single bit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 07:56 AM
  3. Static methods
    By carderne in forum New To Java
    Replies: 10
    Last Post: 01-03-2009, 06:27 PM
  4. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •