• 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

Threads - Bill Brodgen

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all....
This is from Brodgen's 19 Hardest Questions
Given the following listing of the Widget class:
1. class Widget extends Thingee{
2. static private int widgetCount = 0 ;
3. public String wName ;
4. int wNumber ;
5.
6. static synchronized int addWidget(){ widgetCount++ ;
7. wName = "I am Widget # " + widgetCount ;
8. return widgetCount ;
9. }
10. public Widget(){
11. wNumber = addWidget();
12. }
13. }
What happens when we try to compile the class and use multiple Widget objects in a program?
-- options --
a - The class compiles and each Widget will get a unique wNumber and wName reflecting the order in which the Widgets were created.
b - The compiler objects to line 7.
c - A runtime error occurs in the addWidget method.
The answer Given was b because of static method accessing Non-Static variable
But suppose if v change the variable to Static,then what would have been the Answer. i.e. will the answer (a) be the correct Choice.
Thanks
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
choice a) will be the correct answer then.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose its b)
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,
When you will drop the "static" keyword from the <code>addWidget()</code> method then the answer would be
(A).
I guess your querry is answered now.
You may refer the following code and check for yourself.


Ravindra Mohan
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, answer a) says that each Widget gets its own unique number and name. I think you will find that when everything is done running, all of the Widgets created will have the same name (namely, "I am Widget # <last number created>" or whatever). Remember, we changed the wName variable to be static, so it is now shared by all of the Widgets.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, dave is right, each instance that you create will overwrite the name that the previous instance put in the static variable.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic