0

I am very confused between static and not static.

I really can't understand where to use Static variable or static methods and where to use non-static variables or non-static methods?

Waiting for a helping hand

Roman C
  • 49,761
  • 33
  • 66
  • 176
Shantanu Nandan
  • 1,438
  • 8
  • 30
  • 56
  • 10
    You should pick one language at a time. Answers will differ depending on language. – juanchopanza Aug 12 '13 at 14:46
  • 1
    Can't believe this question closed saying off topic – Xinus Aug 12 '13 at 14:49
  • @Xinus "Questions asking for code must demonstrate a minimal understanding of the problem being solved." OP does not demonstrate a minimal understanding. – nanofarad Aug 12 '13 at 14:50
  • @juanchopanza: Or may be retag it as "oop" – Xinus Aug 12 '13 at 14:51
  • @Xinus I don't think that would help. The answers would still differ depending on language. – juanchopanza Aug 12 '13 at 15:03
  • @hexafraction - Perhaps not, but in fairness, the OP is not asking for code. – femtoRgon Aug 12 '13 at 15:15
  • I'm voting to close as too broad, since it does several things, depending on location. You will need to research more on your own first. See e.g. http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class and other SO questions. – djechlin Aug 13 '13 at 01:53
  • 2
    Also downvoting since there are many SO questions on this that you need to review and explain what you don't understand. – djechlin Aug 13 '13 at 01:53

5 Answers5

1

This is for Java. C++ is likely similar in the first two respects, perhaps more, though I cannot be sure.

Static: Applies once for the class. Accessed with FooClass.staticField or FooClass.staticMethod(args).

Non-static: Applies per-instance. Accessed with fooInstance.field or fooInstance.method(). fooInstance is an instance of FooClass and was obtained with a constructor or factory.

Generally, constants such as a Color class's YELLOW constant is declared static final FooClass.

Another use of static is the static initializer:

class FooClass
    static{
        //Code here gets called once when the class is loaded.
    }
nanofarad
  • 40,330
  • 4
  • 86
  • 117
1

In Java, static refers to the class as a whole, not to objects (instantiations) of the class. For example, if I created a class called Bicycle:

  • An instance variable could be its colour, or registration number.
  • However, if every bike has two wheels, number of wheels could be a static variable, as it is class wide (note, this should really be made a constant with the final keyword as well, as it will not change).

It's the same with methods. Static methods are class-wide, non static methods are specific to that object.

Andrew Martin
  • 5,619
  • 10
  • 54
  • 92
  • I would say that a bike's wheel number would be more of a `static final int` or `const`. – nanofarad Aug 12 '13 at 14:49
  • @hexafraction: True, it would be a constant - but would that not still make it static? – Andrew Martin Aug 12 '13 at 14:50
  • Yes. However, it is still good for it to be specified so the OP doesn't need to ask *another* question about constants. – nanofarad Aug 12 '13 at 14:52
  • 1
    True, I'll make that clearer in my answer. – Andrew Martin Aug 12 '13 at 14:52
  • 1
    we can use a static variable in a non static method and can we change the value of static variable ? – Shantanu Nandan Aug 12 '13 at 14:54
  • @user2675355: If you wanted to. You could create a static variable inside a class and create an instance method which alters the variable. Then, in a separate class, instantiate an object of your first class and call that method's instance method to change the variable. – Andrew Martin Aug 12 '13 at 15:00
1

static means slightly different things in C++ and Java, but the rules for static class members (methods and variables) are basically the same: Members that are static apply to the whole class, and members that are not static apply to individual instances of the class.

For a common example, it's usual in Java to have one logger that belongs to the entire class, since each instance doesn't need its own copy. So you see

private static final Logger logger = LoggerFactory.getLogger("loggerName");

(The final here means the same thing as const in C++.)

Methods that are static are ones that don't apply to a specific instance. A common example is Integer.parseInt(String), which isn't called on an existing Integer because it's turning a String into an Integer.

Variables and methods that apply to individual instances (such as length() or name) are not static.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

I dont know how many times this question has to be answered on stackoverflow Accessing Static variables
Firstly read this article,also use wikipedia,then look for a book called Headfirst Java (Sierra & Bates) and also Java How To Program(Deitel,Deitel). These books will be of use to u. After u've read and think u've grasped the concept give urself a project to do where this will be implemented. You will understand it faster and better if u practice so get reading and get coding.

Community
  • 1
  • 1
Manny265
  • 1,709
  • 4
  • 23
  • 42
1

Understand it this way, static variables are shared by all objects unlike non static variables which are different for different objects.Consider a scenario, where you are developing a student class ,different objects of this class will represent different students each having unique roll no, different names(or same). Now, all of this students represent the same college.

class Students
{
int roll;
String name;
int collegecode;
public static void main (String args[])
{
   Students o1= new Students();
   o1.roll =12;
   o1.name ="james";        // This is a bad practice,fields should be private
   o1.collegecode = 7523 ;         
   Students o2 = new Students();
   o2.roll =13;            //  getter, setter methods should be used
   o2.name ="Michael";
   o2.collegecode= 7523;
}

}

Since collegeid would be same for every students (objects), it can be independent of objects and can be made static.Static methods/variables are shared by all objects and can be accessed by classname.variable or classname.method . Although , you can access a static variable/method as object.method or object.variable . (But there is no point in that, you can refer it like o1.staticvariable, o2.staticvariable and so on,it all refers to same variable)

If you make collegecode static in the above example,it will look like

 static int collegecode;

      o1------->  college code  <---- o2
      roll (12)                      roll(13)
      name(James)                    name(Michael)
Malwaregeek
  • 2,274
  • 3
  • 15
  • 18