• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Having doubt on static

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code given i just want to know that whenever i call any method
from the public static void main() it must be static or not,
As it has created some confustion, So far as i have read we cant call
any non-static method or variable from static method..Please elaborate this part

class TestThread extends Thread{
public void restart()
{
startMe();
}
public static void startMe()
{
// Some magic code here
}
public static void main(String[] args)
{
TestThread t1=new TestThread();
t1.start();
t1.restart();
}
}
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rakesh, you can call any non-static methods or fields from static methods (like main) but you need an instance of the class to do that. You can't access non-static members from a static method directly.

Also please Use Code Tags when you post a source code...
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can call static method using the full qualified name:

ClassName.methodName(...)

or using the instance of the class:
new ClassName().methodName();

When you invoke method from within the main method that method must be static if you do not have the instance of the enclosing class.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic