0

I want to know if it is possible to use a static variable inside a non static methode ?

also

can I use a non static variable inside a static methode ?

Thanks

VisaMasterCard
  • 1,666
  • 4
  • 18
  • 22
  • See http://stackoverflow.com/questions/2079830/how-do-i-create-a-static-local-variable-in-java. – Feanor Jan 26 '11 at 23:47
  • 2
    The different between non-static and static method is just having 'this' sent as zero parameter. So non-static methods always effectively have one extra parameter (which is never show in java source, but you can use 'this'). In static there are no extra parameters. Side note: inner classes access private fields of the outer class (and vice verse) by using *static* synthetic methods that accept the reference of the outer (or inner) class. – bestsss Jan 26 '11 at 23:53

5 Answers5

4

A static variable can be accessed from anywhere you like. A non static variable can only be accessed from a non static method or from a specific object (instance of a class).

The reason for this can get quite complicated, but in brief:

Anything that is non static in your class is duplicated whenever an object is instantiated from that class. Anything static is common to all instances of the class (and not duplicated for new objects), meaning it is unaffected by changes in the state of individual objects.

Now obviously until an instance of the class has been created, anything non static cant exist - there's no object for them to belong to. Since static members dont require an instance of a class to exist, it wouldnt be safe for them to access members that do require an instance of an object (non static).

richzilla
  • 40,440
  • 14
  • 56
  • 86
3

Both are possible, but to access an instance (non-static) variable you need an instance.
This can be given implicitly in a non-static context like in an instance method, and must be provided explicitly in a static context.

class StaticOrNot {

    static int staticVar = 1;
    int instVar = 2;

    static void staticMethod() {
        staticVar += 1;
        StaticOrNot someInstance = new StaticOrNot();
        someInstance.instVar += 2;
    }

    void nonStatic() {
        staticVar += 1;
        instVar += 2;  // using this as instance
    }
}
user85421
  • 28,957
  • 10
  • 64
  • 87
1

I want to know if it is possible to use a static variable inside a non static methode ?

Yes.

can I use a non static variable inside a static methode ?

No.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • why I can't use a non static variable inside a static methode ? – VisaMasterCard Jan 26 '11 at 23:48
  • 2
    @Visa: Because it doesn't make any sense. A static method is not associated with a particular instance, whereas a non-static variable is. – Oliver Charlesworth Jan 26 '11 at 23:49
  • You can if you qualify it with an instance: `someInstance.myInstanceVar`. Implicitly it will use `this`, which has no meaning in a static context. – Mark Peters Jan 26 '11 at 23:50
  • @VisaMasterCard: Oli is right, you need an instance of the class to access non-static variables. And inside a static method, you don't have an instance of the current object. – Noon Silk Jan 26 '11 at 23:51
  • @Noon: That should read "instance of the class". "instance" and "object" are synonyms... – Oliver Charlesworth Jan 26 '11 at 23:52
  • @Mark: Your comment is misleading. You can never access a non-static variable inside a static method. You're talking about access the property of some object that you have an instance of. So what? That's obviously possible. – Noon Silk Jan 26 '11 at 23:52
  • @Noon: It's not misleading at all. Are you saying if you have an instance of the object inside of the static method, and you access one of its instance variables, those variables are no longer non-static? It's still an instance variable. Michael got it right. – Mark Peters Jan 26 '11 at 23:54
  • wrote it as comment to the question: inner classes access the *private* fields/methods of the outer class via (package private) static methods of the outer class (that have one parameter, exactly the outer class), thus the 2nd NO is not exactly correct. – bestsss Jan 26 '11 at 23:56
  • @Mark: This is a ridiculous conversation to be having. I won't continue. – Noon Silk Jan 26 '11 at 23:56
  • What is your final decision about this; because I don't understand what are talking about guys. – VisaMasterCard Jan 27 '11 at 00:02
  • @VisaMasterCard: The comments are confusing. Ignore them. The verdict is, you cannot access non-static variables from within a static class. You can, of course, access member variables of any other static or local variable from within a static method. This, IMHO, is so obvious it's not worth mentioning, but the commentors felt the need to mention it. I hope this is clear. The bottom line is: You need an instance of a class to access a non-static variable. Inside a static method, you do not have an instance of the class it is defined in. – Noon Silk Jan 27 '11 at 00:07
1

Think about what it means to use a non-static variable in a static context. A static method is not executing on any instance - therefore, what would it mean to operate on a member field defined on the class? What instance does that field belong to? None!

The opposite scenario, namely, using a static variable in a non-static context makes perfect sense. You're on an instance, and you want to read some static reference that is defined for all instances of a given class.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

I want to know if it is possible to use a static variable inside a non static methode ?

Yes.

can I use a non static variable inside a static methode ?

Only if you have an instance of the class available inside that static method.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720