Static Vs. Private

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • curnicha
    New Member
    • Oct 2007
    • 2

    #1

    Static Vs. Private

    It's been a while since I got down and dirty with some fashioned Java code....

    I am wondering what's the difference between a static and a private method?

    public class Myclass(){

    public static void Main(String args[] str){




    }

    }

    public class myobj(){
    static int c1(){

    Console.WriteLi ne("Hello!");

    }

    private void c2(){
    Console.WriteLi ne("Made it here....");
    }

    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by curnicha
    It's been a while since I got down and dirty with some fashioned Java code....

    I am wondering what's the difference between a static and a private method?

    public class Myclass(){

    public static void Main(String args[] str){




    }

    }

    public class myobj(){
    static int c1(){

    Console.WriteLi ne("Hello!");

    }

    private void c2(){
    Console.WriteLi ne("Made it here....");
    }

    }
    1.) Use code tags when posting code.
    2.) What do the textbooks/tutorials say about static and private?
    3.) That code is C# not Java.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      First, Please don't double post your questions.

      A static method is one that does not depend on any object. The Math class is a perfect example. It is filled with static functions (like Math.random()) and static variables (like Math.PI) that you can use without ever creating a Math object.

      A private method is one that outside classes don't need to see (and, in fact, shouldn't). It can only be called and be used by the object you are using. An example would be a helper function to initialize variables.

      Suppose your class requires 3 member variables. You could have a constructor that forces the user to give all 3 variables when instantiating, or you could provide several different constructors taking different arguments and different amounts, letting your user specify only, say, 2 of these 3 values, and provide a default value for the 3rd.

      Instead of writing 7 different sets of 'nearly' identical code for each constructor, write a private initialize() method that accepts those 3 arguments (which will either be user-defined values or default values) and does the work of initializing the variables.

      Certainly, no other class should be able to call this method - we don't want them messing up the values of these member functions. It should only ever be called from the constructor of that class - so you make it private.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by curnicha
        I am wondering what's the difference between a static and a private method?
        You're asking about two completely unrelated things; your question makes as
        much sense as asking about the difference between nasal hair growth and the
        efficiency of three herrings. Please rethink your question and then rephrase it.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          You're asking about two completely unrelated things; your question makes as
          much sense as asking about the difference between nasal hair growth and the
          efficiency of three herrings. Please rethink your question and then rephrase it.

          kind regards,

          Jos
          ... the efficiency of three herrings?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by r035198x
            ... the efficiency of three herrings?
            Yep, I want to know the difference between them and nasal hair growth ;-)

            kind regards,

            Jos

            Comment

            Working...