• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable inheritance

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends
class B extends Thread
{
static protected int i = 0;
public void run()
{
for(; i<5; i++) System.out.println("Hello");
}
}

public class A extends B
{
public void run()
{
for(; i<5; i++)System.out.println("World");
}
public static void main(String args [])
{
Thread t1 = new B();
Thread t2 = new A();
t2.start(); t1.start();
}
}
My qus is : why it prints total five words not ten words and how to modify this code to prints either five words ("hello") or five words ("world")
Thank in advanced
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because variable i (the loop counter) is shared between A and B. How it executes depends on the underlying platform, but roughly when i increments to 5 then nothing gets printed anymore...
To correct that and print 5 hello's and 5 world's, just remove the static keyword in the declaration of i.
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin:
Thank for your explanation.
But I think class B and class A have their own static var i
(because A is subclass of B --> A inherits i from B)
Could you pls tell me which class static var i belongs to?
Thank in advanced
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static stuff are not inherited..they can only be shadowed.
HTH
Shyam
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
like Valentin Crettaz was saying "variable i (the loop counter) is shared between A and B" and since static is not inherited
i belongs to Class B.
"But I think class B and class A have their own static var i"
No, static members belong to the class and they aren't
inherited. So, A does not have another var i.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic