Hi!
I am taking a java class and it is teaching me how to write the code where I am calling a method from another class. It is giving me an error
below is the code and the error - Can anyone help me with this?
Thanks!!
public class TestStatement
{
public static void main(String[] args)
{
System.out.println("Calling method from another class:");
SetUpSite.statementOfPhilsophy();
}
}
C:\Documents and Settings\Nicole\My Documents\TestStatement.java:6: cannot resolve symbol
symbol : method statementOfPhilsophy ()
location: class SetUpSite
SetUpSite.statementOfPhilsophy();
^
1 error
Having a problem calling a method from another class
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
In an effort to redeem myself for my flubbed document selector answer, I'd say what you should be checking for is that you have the method call capitalized or identified correctly.
Doing some quick testing, if the method wasn't static, it'd give an error about ``non-static method statementOfPhilsophy() cannot be referenced from a static context.'' A permission issue would usually be identified as such (statementOfPhilsophy() has private access in SetUpSite). If it's misspelled or incorrectly capitalized, it'd be ``cannot resolve symbol.''
and
Doing some quick testing, if the method wasn't static, it'd give an error about ``non-static method statementOfPhilsophy() cannot be referenced from a static context.'' A permission issue would usually be identified as such (statementOfPhilsophy() has private access in SetUpSite). If it's misspelled or incorrectly capitalized, it'd be ``cannot resolve symbol.''
Code: Select all
public class TestStatement
{
public static void main(String[] args)
{
System.out.println("Calling method from another class:");
SetUpSite.statementOfPhilsophy();
}
}
Code: Select all
class SetUpSite
{
static void statementOfPhilsophy()
{
System.out.println("foo");
}
}
I choose to fight with a sack of angry cats.
the method in the other class is declared public and I have checked the case in the original code and made sure when I am trying to call to that method I am typing it exactly as it appears in the main class.
Thanks for your help. I wasn't sure if maybe I had to use a different syntax in Textpad or not
Thanks for your help. I wasn't sure if maybe I had to use a different syntax in Textpad or not
Nikki
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
Why don't you give this a whirl? What it'll do is create an instance of the class SetUpSite and then it'll look and see what methods that class says it has. Let us know what it says. If not, if you can just go ahead and post the SetUpSite code (if it's not too terribly long).
Heck if nothing else, it's good for me to look at compiled languages from time to time even if I think Python and SQL are so delicious.
Heck if nothing else, it's good for me to look at compiled languages from time to time even if I think Python and SQL are so delicious.
Code: Select all
import java.lang.reflect.Method;
public class TestStatement
{
public static void main(String[] args)
{
System.out.println("Calling method from another class:");
//SetUpSite.statementOfPhilsophy();
String className = "SetUpSite";
try
{
Class c = Class.forName(className);
Method [] m = c.getDeclaredMethods();
for (int i = 0; i < m.length; i += 1)
System.out.println(m[i]);
}
catch (ClassNotFoundException ex)
{
System.out.println("Class not found " + className);
}
}
}
I choose to fight with a sack of angry cats.