Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple java question...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I just started learning java and wanted to make a simple program with a main and an init() method. Isn't it possible to start a method from a mainfunction? I'm getting the error: can't make a static reference to method void init()in class Test. Here is the code:

class Test
{
public static void main(String args[])
{
init();
}

public void init()
{
System.out.println("Starting init()");
}
}

So how do I call the method init() without having to erase static from the main?

Thx
Hartge
 
Sorry my mistake, make the method "public static void init()" becours you don't declare an object for test you hava to say make it static so I can use just the function.

Charl
 
Hi,

Reason why the compiler complains is because a static method cannot reference to a non-static method wheras a non-static method can reference a static method. The same goes for variables.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top