5

Possible Duplicate:
When should a method be static?

Usually when writing a static method for a class, the method can be accessed using ClassName.methodName. What is the purpose of using 'static' in this simple example and why should/should not use it here? also does private static defeat the purpose of using static?

public class SimpleTest { 

   public static void main(String[] args) {
         System.out.println("Printing...");
         // Invoke the test1 method - no ClassName.methodName needed but works fine?
         test1(5);
   }

   public static void test1(int n1) {
         System.out.println("Number: " + n1.toString());
   }
   //versus
   public void test2(int n1) {
         System.out.println("Number: " + n1.toString());
   }
   //versus
   private static void test3(int n1) {
         System.out.println("Number: " + n1.toString());
   }
}

I had a look at a few tutorials. E.g. http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

My understanding of it is that instead of creating an instance of a class to use that method, you can just use the class name - saves memory in that certain situations there is no point in constructing an object every time to use a particular method.

Community
  • 1
  • 1
Mercury
  • 711
  • 1
  • 11
  • 21
  • 4
    How much do you understand about what `static` means? Have you read a book or tutorial? Perhaps you could edit your question to explain what you believe about its meaning to start with. – Jon Skeet Jan 23 '13 at 20:28
  • 1
    @KingsIndian: In my opinion, this is not a duplicate. The linked question asks for rules when to make methods static in general. This question talks about a specific code sample and asks about the reason for using static in this particular code sample, also with respect to different access modifiers. – O. R. Mapper Jan 23 '13 at 21:46
  • http://stackoverflow.com/questions/538870/java-static-methods-best-practices?rq=1 – P.P Jan 23 '13 at 22:01
  • http://stackoverflow.com/questions/658407/static-methods?lq=1 – P.P Jan 23 '13 at 22:01
  • http://stackoverflow.com/questions/1530353/in-what-situations-is-static-method-a-good-practice?lq=1 – P.P Jan 23 '13 at 22:02
  • http://stackoverflow.com/questions/5762004/is-there-any-use-of-static-methods-functions-in-java-other-than-ease-of-calling?lq=1 – P.P Jan 23 '13 at 22:03
  • @O.R.Mapper I added few other links and there are plenty more.. IMO this is a basic question about `static methods` and a decent understanding of it should be enough to answer it by OP himself. One can have create as many variations with `static methods` but not all are going to be unique questions... If you believe the linked questions don't answer enough, I'll delete my comments and come back to cast re-open vote in case this gets closed. – P.P Jan 23 '13 at 22:04

8 Answers8

4

The purpose of the static keyword is to be able to use a member without creating an instance of the class.

This is what happens here; all the methods (including the private ones) are invoked without creating an instance of SimpleTest.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
1

static means that the function doesn't require an instance of the class to be called. Instead of:

SimpleTest st = new SimpleTest();
st.test2(5);

you can call:

SimpleTest.test1(5);

You can read more about static methods in this article.

A question about private static has already been asked here. The important part to take away is this:

A private static method by itself does not violate OOP per se, but when you have a lot of these methods on a class that don't need (and cannot*) access instance fields, you are not programming in an OO way, because "object" implies state + operations on that state defined together. Why are you putting these methods on that class, if they don't need any state? -eljenso

Community
  • 1
  • 1
Foggzie
  • 9,691
  • 1
  • 31
  • 48
1

In this Example,Static is used to directly to access the methods.A private static method defeats the purpose of "Data hiding".

Your main can directly call test1 method as it is also Static,it dosn't require any object to communicate.Main cannot refer non-static members,or any other non-static member cannot refer static member.

"non-static members cannot be referred from a static context"

You can refer This thread for more info about Static members.

joey rohan
  • 3,505
  • 5
  • 33
  • 70
0

static means that the method is not associated with an instance of the class.

It is orthogonal to public/protected/private, which determine the accessibility of the method.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You put in static methods all the computations which are not related to a specific instance of your class.

About the visibility, public static is used when you want to export the functionality, while private static is intended for instance-independent but internal use.

For instance, suppose that you want to assign an unique identifier to each instance of your class. The counter which gives you the next id isn't related to any specific instance, and you also don't want external code to modify it. So you can do something like:

class Foo {
    private static int nextId = 0;

    private static int getNext () {
        return nextId ++;
    }

    public final int id;

    public Foo () {
        id = getNext();    // Equivalent: Foo.getNext()
    }
}

If in this case you want also to know, from outside the class, how many instances have been created, you can add the following method:

public static int getInstancesCount () {
    return nextId;
}

About the ClassName.methodName syntax: it is useful because it specifies the name of the class which provides the static method. If you need to call the method from inside the class you can neglect the first part, as the name methodName would be the closest in terms of namespace.

Dacav
  • 13,590
  • 11
  • 60
  • 87
0

Calling test1 from main in your example works without using the class name because test1 is a static method in the same class as main. If you wanted to call test2 from main, you would need to instantiate an object of that class first because it is not a static method.

SShaheen
  • 1,012
  • 7
  • 21
  • Am I right in saying that any static method in a class can be used in any other static method in a class without the need to use the class name? Also, if I said this.test1(5) or SimpleTest.test1(5) would that be considered valid? – Mercury Jan 23 '13 at 20:50
  • I haven't tested these cases, but this is what I would assume: Yes, you can use any other static method in the class without the class name, as long as that method name does not mean something else in the context of the method you are in. I would use the class name with the method to be safe and clear (SimpleTest.test1(5)). I do not believe this.test1(5) will work, because "this" does not refer to anything in this context. There is no SimpleTest object. – SShaheen Jan 23 '13 at 21:50
0

A static method does not need to be qualified with a class name when that method is in the same class. That a method is private (static or not) simply means it can't be accessed from another class. An instance method (test2 in your example) can only be called on an instance of a class, i.e:

new SimpleTest().test2(5);
0

Since main is a static method, if you want to call a method of the class without having to instantiate it, you need to make those methods also static.

In regards to making a private method static, it has more readability character than other. There isn't really that much of a difference behind the hoods.

Gothmog
  • 871
  • 1
  • 8
  • 20