Simple java method problem
I have a problem with my program. I am trying to call a method from a secondary class in the main class.
Whenever I debug the Test class I only get the text output "hello, Im the Test class!" or "hello Im the TestCall Class!" when I run the TestCall class.
I want it to display either one of the two text outputs and the text "I am method showMessage from class Test!"
This is the main class
Code Java:
public class Test{
public static void main(String args[])
{
System.out.println("hello, Im the Test class!");
}
public void showMesage(){
System.out.println("I am method showMessage from class
Test!");
}
}
And this is the secondary class:
Code Java:
public class TestCall{
public static void main(String args[])
{
System.out.println("hello Im the TestCall Class!");
}
public void run(){
Test t = new Test(); // create an instance
t.showMesage(); // use t to refer any method of class Test
}
}
The first code I made had no:
Code Java:
public static void main(String args[])
{
System.out.println("hello Im the TestCall Class!");
}
Then I received an error message when I tried to debug it: "Exception in thread "main" java.lang.NoSuchMethodError: main"
Re: Simple java method problem
See Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects) To call a method, you must call the method. For example, in the second to last piece of code, call run() in the main method to run that piece of code.
Re: Simple java method problem
I tried to do it this way but it still wont work.
Main class:
Code Java:
public class Test
{
public static void main(String args[])
{
System.out.println("hello Im the Test Class!");
}
public void run(){
TestCall t = new TestCall(); // create an instance
t.showMesage(); // use t to refer any method of class Test
}
}
Secondary class:
Code Java:
public class TestCall
{
public static void main(String args[])
{
System.out.println("hello, Im the TestCall class!");
showMesage();
}
public static void showMesage()
{
System.out.println("I am method showMessage class
TestCall!");
}
}
What I just did was compile the secondary class first, then i compiled the main class and ran the main class. It still wont call the method.
I'm sure I'm doing a lot wrong here....
And thanks for your reply
Re: Simple java method problem
I'm not sure what you wish to run...both classes have a main method. Stick to a single main method and run the code from there.
Re: Simple java method problem
Okay... So I finally got it to work. I don't know though if this is the appropriate way of doing it.
Here's my code, please tell me what you think.
Main class:
Code Java:
public class Test extends TestCall
{
public static void main(String args[])
{
Test nt=new Test();
TestCall t = (TestCall)nt;
t.showMesage();
}
}
Secondary class:
Code Java:
public class TestCall
{
public static void showMesage()
{
System.out.println("I am method showMessage class
TestCall!");
}
}
Re: Simple java method problem
There is no reason for Test to extend TestCall.
Code java:
public class Test {
public static void main(String args[]) {
TestCall t = new TestCall();
t.showMesage();
}
}
Also, the showMessage method should not be static.