8 Replies Latest reply: Feb 26, 2007 1:38 AM by 807599 RSS

    what is the benefit of static in a method/function

    807599
      hello,

      can u explain the benefit of static in a function?
      and why i cannot called a function in a static method..example :
      class jalaninFungsi{
      
           
           public static void main(String args[])
           {
      
           
                tesfungsi();
      
           }
           
            public  void tesfungsi()
           {
                System.out.println("ini tesfungsi");
           }          
      }
      its produce error : non-static method tesfungsi() cannot be referenced from static context

      thanks.
        • 1. Re: what is the benefit of static in a method/function
          807599
          The use of static cannot be explained in a post or in a thread on a forum. You need to read about the subject in a tutorial/elsewhere.

          Simply stated, static methods can be called without instantiating the class which contain them.

          As for the benefits, well - like I said, you need to read up on usage before you decide what the "benefits" are. (There are no pros/cons to using them. They were designed for a specific usage. You either use them proproperly, or you don't.)
          • 2. Re: what is the benefit of static in a method/function
            3004
            can u explain the benefit of static in a function?
            and why i cannot called a function in a static
            method..example :
            A static method exists at the class level whereas a non-static method exists within an object (and that's why they're also called instance methods).

            Static methods can be used to represent information which concerns the class as a whole.
            • 3. Re: what is the benefit of static in a method/function
              807599
              An example of some static methods in the Java API are "Thread.sleep" and "Math.random" These methods are static which means you do not need to instantiate the class to use them. All you need to do is specify the class name ie Thread or Math followed by the method you wish to use sleep or random. The benefit of this is as I mentioned you don't need to create an instance of the thread class to use them. If Sleep was not static you would have to do something like this to use it's methods.
              Thread t = new Thread();
              t.sleep(1000);  //sleep for one second
              As Thread.sleep is static you can simply do this.
              Thread.sleep(1000);
              As NavyCoder pointed out you won't fully understand this in a forum but you will come to understand this in time. All you need to know for now is when working from main as you are now is that you will need to create an instance of the class like this:
              jalaninFungsi jf = new jalaninFungsi(); //creates an instance of your class to work with rather than the class (the blueprint) itself
              jf.tesfungsi();
              That will remove you error.

              Mike
              • 4. Re: what is the benefit of static in a method/function
                807599
                thanks for the info...its very helpful
                • 5. Re: what is the benefit of static in a method/function
                  807599
                  class jalaninFungsi{

                       
                       public static void main(String args[])
                       {

                       
                            tesfungsi();

                       }
                       
                       public void tesfungsi()
                       {
                            System.out.println("ini tesfungsi");
                       }          
                  }
                  • 6. Re: what is the benefit of static in a method/function
                    807599
                    class jalaninFungsi{

                         
                         public static void main(String args[])
                         {
                    jalaninFungsi jf = new jalaninFungsi();
                         
                              jf.tesfungsi();

                         }
                         
                         public void tesfungsi()
                         {
                              System.out.println("ini tesfungsi");
                         }          
                    }
                    • 7. Re: what is the benefit of static in a method/function
                      3004
                      An example of some static methods in the Java API are
                      "Thread.sleep" and "Math.random" These methods are
                      static which means you do not need to instantiate the
                      class to use them. All you need to do is specify the
                      class name ie Thread or Math followed by the method
                      you wish to use sleep or random. The benefit of this
                      is as I mentioned you don't need to create an
                      instance of the thread class to use them.
                      Not having to instantiate is not so much "what static means" or "the 'benefit' of static." It's more of a secondary effect. It's not like creating objects is something onerous that is to be avoided if possible.

                      What static means is that it's associated with the class as a whole, not with individual instances of the class. One implication of that is that you don't need to access it through an instance (and shouldn't--use the class name). But that's not the "meaning" or "benefit."

                      As stated, static methods don't have a "benefit" over non-statics any more than a hamer has a benefit over a screwdriver. They're just different tools for different jobs.
                      • 8. Re: what is the benefit of static in a method/function
                        807599
                        A class is an object.

                        So for example:

                        class STUDENT
                        {
                        public String getStudentName()
                        {
                        return name;
                        }

                        public int getStudentGPA()
                        {
                        return GPA;
                        }
                        }

                        In this case, to be able to call the function getStudentName() and getStudentGPA() you will actually need a STUDENT object.
                        That means you must have declared e.g. STUDENT A; (in the main method for example), then to call those two functions above you'll need to write A.getStudentName(); and A.getStudentGPA();

                        HOWEVER if you have static method, you don't need to create an object of that class type. You can just simply call the class name and then use the dot operator and call the function. e.g.:

                        class STUDENT
                        {
                        public static String getStudentName()
                        {
                        return name;
                        }

                        public static int getStudentGPA()
                        {
                        return GPA;
                        }
                        }

                        Notice that these are static methods, so to access these methods you just need to write STUDENT.getStudentName(); or STUDENT.getStudentGPA();
                        Again, notice that here I don't use A . but instead I used the class name (STUDENT.)

                        However, this is not logically correct in this (my) particular case to make those two methods static. You'll need to read more by yourself about the usage of static method. I am just trying to explain it to you why there is such thing called static methods in java.

                        - o.z.