Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 20-01-2010, 10:07 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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:
Java Code
public class Base
{
     public static int x = 5;
}
Java Code
public class Derived extends Base
{
     static
     {
          x = 3; // this is changing Base's x
     }
}
Java Code
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:

Java Code
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.

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



__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote Share this thread on Facebook
The Following User Says Thank You to helloworld922 For This Useful Post:
JavaPF (21-01-2010)
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 22-01-2010, 08:02 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I set a static variable?? wingchunjohn Object Oriented Programming 4 22-01-2010 08:36 AM
Static to non-static - Organization copeg Object Oriented Programming 5 22-12-2009 05:56 PM
static final object kalees Object Oriented Programming 3 21-12-2009 04:29 PM
Problem with OOP - Inheritance connex Object Oriented Programming 1 15-12-2009 03:11 AM
Static method kalees What's Wrong With My Code? 4 20-11-2009 03:10 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:53 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.