0

I'm quite new to Java and I'm trying to understand static variables and methods. Could someone give me a brief explanation of why I'd use the static method over the instance method and also what would be the ideal situation to use static variables? I know that static variables and methods are part of the class and not an instance of said class but I'm having trouble understanding when I'd use it.

Thanks!

Spara1008
  • 21
  • 1

3 Answers3

0

These are the main differences

1.Static variables take memory only 1 time during the lifetime of program.
2.You can call static variables or methods. without creating the object of that class by classname only.
3.Static variables can be called in static and non-static context.

Mainly static variables are used to save memory because every object takes memory .

TruePS
  • 493
  • 2
  • 12
  • 33
  • `To save memory`? I doubt that. Java's GC can not act against static variables while it can easily release normal memory when it is not used. Statics can cause memory problems it you don't use them wisely. – Ean V Apr 23 '14 at 05:36
  • @Ean i totally agree with you `Statics can cause memory problems it you don't use them wisely`.But i have 1 question: if i make 10 objects of a class and there is only 1 variable in that class say int x.so when i will call the same non-static variable via these 10 objects won't that non-static variable takes memory 10 times one for each object. – TruePS Apr 23 '14 at 05:40
  • How does your answer expand on, or add to, the well established Q and A this question is a Dup of? – Brian Roach Apr 23 '14 at 05:41
  • @Brian look at the timings first i was the first person to answer the question.So i can say the others are duplicate answers and i didn't see at that time it is a Dup of. – TruePS Apr 23 '14 at 05:42
  • 1
    It doesn't appear your answer was given prior to Jan 2009. – Brian Roach Apr 23 '14 at 05:43
  • @Brian if the OP was unable to get what he was looking for then we should help him.That is what SO policies say. – TruePS Apr 23 '14 at 05:45
  • Actually, no, that's the whole reason we have close votes for duplicates. Which this Q should be closed for soon. – Brian Roach Apr 23 '14 at 05:46
  • @Brian ok I will be more careful in the future but I read some article in meta SO saying that we should answer duplicate questions as well to help OP's. – TruePS Apr 23 '14 at 05:47
0

Static members relate to a type not a concrete instance. So instance methods and variables concern the identity of the instance, e.g. an instance method can alter the state of the object.

To some extend this is a religious question. Some people prefer to create static methods in a class when they do not access any instance variable. Other people do not like this approach. Another usual usage is creating factory methods, able to create an instance of an object while itself being called statically.

Often there are utility classes that contain static methods to serve clients with some functionality that is not bound to any type context. But all too often these utility classes get abused by using them as a trash bin for all sorts of methods a programmer did not find a fitting class for. This may be a result of poor design.

A popular use of static variables is the definition of an logger instance (e.g. SLF4J) for a class because loggers are thread safe and can be used by all instances and static methods alike. One advantage is (see a comparison) that only one instance has to be created for all instances of the containing class.

mdo
  • 6,961
  • 3
  • 24
  • 26
  • How does your answer expand on, or add to, the well established Q and A this question is a Dup of? – Brian Roach Apr 23 '14 at 05:42
  • @BrianRoach: you mean Boris' linked SO? I think my answer adds some insight to the "when to use" OP asked for, in contrast to the "what is it". But btw: how am I generally supposed to find the "well established Q and A"? – mdo Apr 23 '14 at 05:59
  • *'how am I generally supposed to find the "well established Q and A"?'* If you are savvy you type `[java] [static]` in the search box and it's on the first page sorted by votes. – Radiodef Apr 23 '14 at 06:07
  • @BrianRoach, Radiodef: I re-read the referenced SO but have a strong feeling that the OP's "when to use"-part isn't really answered there. While I don't want to say that my answer is a comprehensive essay on the topic, are users supposed to add answers like mine to the well established QA that really doesn't ask for the "when"? It seems to be more a dup of this QA: http://stackoverflow.com/q/2671496/492462 -- isn't it? – mdo Apr 23 '14 at 06:34
0

For static variable or method, you can think of that there is only one copy of the variable or method during the whole life time of the program.

So if you there is variable whose value should be shared across all the processes or thread, you can declare a static variable. For example, when you want to use a singleton pattern, you will need a static variable to indicate if there is instance of the class already.

For static methods, I would use them when state of the class is not important. I think the best example is java.lang.Math, they are all static methods, and returns the expected values to you no matter what you called before.

Dino Tw
  • 3,167
  • 4
  • 34
  • 48