1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

static concept : variable and method in class defination

Discussion in 'C' started by mocha, Oct 25, 2010.

  1. mocha

    mocha New Member

    Joined:
    Oct 24, 2010
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0

    hello every body ,
    what is the answer for thees questions ?
    When the keyword static is used on variable in a class definition, can the variable be altered in a non static method? Why or why not?

    when the keyword static is used on a method in a class definition, can the method alter non-static variables in the class? why or why not?
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    1. Yes, because the object exists when you invoke the non-static method, so there's no problem there.

    2. No, because the object might not exist within the static function, so it isn't allowed to modify any non-static class members.
     
  3. mocha

    mocha New Member

    Joined:
    Oct 24, 2010
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0
    great.I understand many things by your answer.
    Thanks a lot.
     

Share This Page