Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Static fields and inheritance

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Static fields and inheritance

    Setup: Java SE 6. Previous/future versions of Java may be different, I haven't tried them out yet.

    If you declare a static variable, by default it doesn't get inherited:
    public class Base
    {
         public static int x = 5;
    }

    public class Derived extends Base
    {
         static
         {
              x = 3; // this is changing Base's x
         }
    }

    public class Main
    {
         public static void main(String[] args)
         {
              System.out.println(Derived.x); // Will spit out an error, need to access the method statically, or from Base
              System.out.println(Base.x); //  prints out "3" because the static Derived got built second
         }
    }

    However, if you have your Derived class declare a static variable x:

    public class Derived extends Base
    {
         public static int x; // may get a warning about variable hiding
         static
         {
              x = 3; // Derived's x is change, Base x is still 5
         }
    }

    Then you can use the two instance variables separate denoted by the class name. Note that from this method, it's not even necessary to force the two variables to the same type.

    public class Derivved extends Base
    {
         public static String x = "Hello"; // again, there may be a warning about variable hiding
    }

  2. The Following User Says Thank You to helloworld922 For This Useful Post:

    JavaPF (January 21st, 2010)


  3. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Static fields and inheritance

    Ooooh, I'd be very careful trying to make statics inherited because that's not really the idea. This one can be a real head case.

    // Json

Similar Threads

  1. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 04:36 AM
  2. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 01:56 PM
  3. static final object
    By kalees in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:29 PM
  4. Problem with OOP - Inheritance
    By connex in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2009, 11:11 PM
  5. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM