This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method call from non static method

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • 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
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic