• 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

Static method call from non static method

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is my understanding that static methods can't access non-static methods or non-static instance variables, however I wanted to be sure I'm clear in the opposite case, specifically calling a static method from within a non static method. Also, I'm pretty sure static variables may be incremented from a non-static method, ie constructor(). So the question I think really has to do with calling a static method from within a non static method:
class Avg2 extends Thingee
{
static private int WidgetCount = 0;
public string wName;
int wNumber;
private static synchronized in addWidget()
{
return ++Widgetcount;
}
public Avg2()
{
wNumber = addWidget()
}
}
Thanks in advance for your help
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yuo are correct in your assumption.
no problem calling static methods or variables from non static methods.
the opposite you cant as you mentioned.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your insight. I believe one of the mock exams may have an error indicating that a "call to a static method from a non-static method results in a compiler error". Perhaps I could see some error occuring if wNumber were private?
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic