Static Members in a Class/Interface

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Static Members in a Class/Interface

    What would be the better, whether should i access the static members using class name or using Object? Which one is preferred(low-cost effective)?

    Today a thing stroke into my mind... Java supports multiple Interface Implementation but only single class extension to avoid ambiguous reference(data/members). Java manages to avoid method ambiguity but can't data ambiguity ;) Then the question stroke my mind static members should use using class name. But i am not sure which one is low-cost effective?

    Code:
    public class Test {
        int a = 100;
    }
    
    interface InterfaceTest{
        int a = 100;
    }
    
    class SubTest extends Test implements InterfaceTest{
        SubTest(){
            System.out.println("A: " + a); //Wrong one (ambiguous reference)
            System.out.println("A: " + InterfaceTest.a); //Right one
        }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Use the class name always. Anyone looking at the code will be able to tell it's a static just by how you called it.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by r035198x
      Use the class name always. Anyone looking at the code will be able to tell it's a static just by how you called it.
      This is what you telling about coding convention.
      My question was ..which one was cost-effective?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by dmjpro
        This is what you telling about coding convention.
        My question was ..which one was cost-effective?
        I think you meant to write your example as:

        Code:
        public class Test { 
            static int a = 100; 
        }
        ... and as said before always write Test.a, not o.a where o is an object of class Test. Also it makes more clear which static member you want. Compare the following (obfuscated) code snippet:

        Code:
        class B {
        	public static int a= 42;
        }
        
        class D extends B {
        	public static int a= 54;
        }
        
        public class Test {
        	public static void main(String[] args) {
        
        		B x= new D();
        		
        		System.out.println(x.a);
            }
        }
        What will be printed? 42 or 54? If you had written B.a or D.a it would've been clear immediately. There are no performance issues, better spend your energy on useful stuff (check it out with javap).

        kind regards,

        Jos

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by JosAH
          What will be printed? 42 or 54? If you had written B.a or D.a it would've been clear immediately. There are no performance issues, better spend your energy on useful stuff (check it out with javap).
          Obviously the print will be 42. So you are telling that there is no performance issue..right! For coding convention it's better to use class name.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Sorry to jump in.

            Jos, can you explain to me why your last code-snippet would return 42.

            Thanks.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Markus
              Sorry to jump in.

              Jos, can you explain to me why your last code-snippet would return 42.

              Thanks.
              Local variable x is of type B (I declared it that way so that's all the compiler knows) so x.a is B's static member variable a which is 42.

              kind regards,

              Jos

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by JosAH
                Local variable x is of type B (I declared it that way so that's all the compiler knows) so x.a is B's static member variable a which is 42.

                kind regards,

                Jos
                So, if you create a new object of x, where x extends y, with the type of y, the new object will take on the members/properties of it's type (y) , not the class it created an object from (x)?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Markus
                  So, if you create a new object of x, where x extends y, with the type of y, the new object will take on the members/properties of it's type (y) , not the class it created an object from (x)?
                  Yep, and the same counts for non-static members; remove the 'static' keyword in both classes in my example and you get 42 as output as well. The compiler decides which variable to use and from the 'outside world' the compiler knows no better than that x is a B; data members don't override they can only be hidden in the 'inside world' i.e. in code inside a method of a class.

                  Not so with method members: they are overridden ('virtual' functions in C++). That's why public data members are a no-no in object oriented programming. But, if the design of the language would've been such that data members were also overridden, the vft (Virtual Function Table) needs to contain pointers to all data members of (an object of) a class as well.

                  kind regards,

                  Jos

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by JosAH
                    Yep, and the same counts for non-static members; remove the 'static' keyword in both classes in my example and you get 42 as output as well. The compiler decides which variable to use and from the 'outside world' the compiler knows no better than that x is a B; data members don't override they can only be hidden in the 'inside world' i.e. in code inside a method of a class.

                    Not so with method members: they are overridden ('virtual' functions in C++). That's why public data members are a no-no in object oriented programming. But, if the design of the language would've been such that data members were also overridden, the vft (Virtual Function Table) needs to contain pointers to all data members of (an object of) a class as well.

                    kind regards,

                    Jos
                    Data members are overridden, but member methods aren't. Understood.

                    Thanks, Jos.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Markus
                      Data members are overridden, but member methods aren't. Understood.

                      Thanks, Jos.

                      Nonono, the terminology is: data members can be hidden and method members can be overridden; the first is a compiler issue, the second one is handled during runtime.

                      kind regards,

                      Jos

                      Comment

                      • dmjpro
                        Top Contributor
                        • Jan 2007
                        • 2476

                        #12
                        Originally posted by dmjpro
                        Obviously the print will be 42. So you are telling that there is no performance issue..right! For coding convention it's better to use class name.
                        Here is an another example ;)

                        Code:
                        public class Test {
                            public static void main(String a[]){
                                Derived d = new Derived();
                            }
                        }
                        
                        class Super{
                            static int a = 100;
                        }
                        
                        class Derived extends Super{
                            static int a = 200;
                            Derived(){
                                System.out.println(super.a);
                            }
                        }
                        So confusing ..but i have a warning "accessing static field".

                        Comment

                        • dmjpro
                          Top Contributor
                          • Jan 2007
                          • 2476

                          #13
                          Originally posted by Markus
                          Data members are overridden, but member methods aren't. Understood.

                          Thanks, Jos.
                          One more thing static members(only methods, as said my Josh ;) ) don't get overridden.

                          Comment

                          • dmjpro
                            Top Contributor
                            • Jan 2007
                            • 2476

                            #14
                            Originally posted by JosAH
                            Nonono, the terminology is: data members can be hidden and method members can be overridden; the first is a compiler issue, the second one is handled during runtime.

                            kind regards,

                            Jos
                            Have a look at this example @Markus

                            Code:
                            class Super{
                                int b = 100;
                            }
                            
                            class Derived extends Super{
                                int b = 200;
                            }
                            
                            //Main Program.
                            public static void main(String a[]){
                                    Super d = new Derived();
                                    System.out.println("The value of b: " + d.b);
                                }

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by dmjpro
                              Have a look at this example @Markus
                              Don't make this an obfuscated thread; we were discussing static members. Don't throw in a completely other example or add a bit of explanatory text or a clear question.

                              kind regards,

                              Jos

                              Comment

                              Working...