Having a problem calling a method from another class

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
nikki77
Posts: 2
Joined: Sat Jan 17, 2004 9:18 pm

Having a problem calling a method from another class

Post by nikki77 »

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
Nikki
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

Is the method in the other class declared static and public?
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

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.''

Code: Select all

public class TestStatement
{
   public static void main(String[] args)
   {
      System.out.println("Calling method from another class:");
      SetUpSite.statementOfPhilsophy();
   }
}
and

Code: Select all

class SetUpSite
{
   static void statementOfPhilsophy()
   {
      System.out.println("foo");
   }
}
I choose to fight with a sack of angry cats.
User avatar
nikki77
Posts: 2
Joined: Sat Jan 17, 2004 9:18 pm

Post by nikki77 »

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
Nikki
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

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.

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.
tat
Posts: 3
Joined: Sun Jan 25, 2004 1:12 am
Location: nj

Post by tat »

make sure that your method is declared as static. Than you don't need to create an instance of this class to call a method
Post Reply