Have a problem on static modifier

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javalover
    New Member
    • Aug 2007
    • 3

    Have a problem on static modifier

    public class X {
    2. private static int a;
    3.
    5. public static void main (String[] args) {
    6. modify (a);
    7. }
    8.
    9. public static void modify (int a) {
    10. a++;
    11. }

    why it is printing '0" ..as per the question int a should get increased and print 1
    I am not understanding this
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by javalover
    public class X {
    2. private static int a;
    3.
    5. public static void main (String[] args) {
    6. modify (a);
    7. }
    8.
    9. public static void modify (int a) {
    10. a++;
    11. }

    why it is printing '0" ..as per the question int a should get increased and print 1
    I am not understanding this
    The value of the parameter 'a' is incremented, it had the same value as the
    value of the private static int member with the same name. Because you didn't
    explicitly initialize that static member it has the value zero.

    Java passes parameters by value; always; no exception to the rule.

    I don't understand why you think anything is printed at all.

    kind regards,

    Jos

    Comment

    • praveen2gupta
      New Member
      • May 2007
      • 200

      #3
      Originally posted by javalover
      public class X {
      2. private static int a;
      3.
      5. public static void main (String[] args) {
      6. modify (a);
      7. }
      8.
      9. public static void modify (int a) {
      10. a++;
      11. }

      why it is printing '0" ..as per the question int a should get increased and print 1
      I am not understanding this

      Hi
      a++ operator say the print first and then increment. So change your line no 10 as below program.
      a is initilized to 0 and when class and static method are loaded with it's value is first loaded to 0 after that it is incremented so you are getting it 0.


      Code:
      public class X 
      {
      private static int a;
      
      public static void main (String[] args) 
      	{
      	modify (a);
      	}
      
       public static void modify (int a) 
      {
       System.out.println(++a);
       }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        ... and the static modifier is not the problem here.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          ... and the static modifier is not the problem here.
          As a matter of fact: if 'a' weren't a *static* member, the entire thing wouldn't even
          compile.

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            As a matter of fact: if 'a' weren't a *static* member, the entire thing wouldn't even
            compile.

            kind regards,

            Jos
            I know but the OP's problem of a not incrementing is really because of the passing by value (and possibly the hiding) rather than the static modifier. The title of the thread would give a different impression.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              I know but the OP's problem of a not incrementing is really because of the passing by value (and possibly the hiding) rather than the static modifier. The title of the thread would give a different impression.
              Yep, as I already wrote in post #2 ...

              kind regards,

              Jos

              Comment

              Working...