calling a method from a static method

hello all,I'm calling a non-static method from a static method (from the main method). To overcome this i can make the method i am calling static but is there another way to get this to work without making the method that is being called static?all replies welcome, thanks
[294 byte] By [abshirf2a] at [2007-11-26 22:20:31]
# 1

You need to call it on some object.

For example:

class A

{

public void doStuff()

{

System.out.println("A!");

}

public static void main(String[] args)

{

doStuff(); //<Bad!

/* Good! */

A a = new A();

a.doStuff();

}

}

CaptainMorgan08a at 2007-7-10 11:17:35 > top of Java-index,Java Essentials,Java Programming...
# 2

When you call a non-static method, you are saying you are calling a method on an object. The object is an instance of the class in which the method is defined. It is a non-static method, because the instance holds data in it's instance variables that is needed to perform the method. Therefore to call this kind of method, you need to get (or create an instance of the class. Assuming the two methods are in the same class, you could do

public class Foo

{

public static void main(String[] args)

{

Foo f = new Foo();

f.callNonStaticMethod();

}

}

for instance.

dmbdmba at 2007-7-10 11:17:35 > top of Java-index,Java Essentials,Java Programming...
# 3
Thank you very much!I created an instance of a class and have called the method like this: time.getTime();I cant help thinking though i have been told this but i don't remember! But thank you once again.
abshirf2a at 2007-7-10 11:17:35 > top of Java-index,Java Essentials,Java Programming...