Hi, I wrote my own method in Java, and I have it in it's own class. I am trying to call it from another class in the same package, but it gives me the error 'symbol not found'. What did I do wrong?
Printable View
Hi, I wrote my own method in Java, and I have it in it's own class. I am trying to call it from another class in the same package, but it gives me the error 'symbol not found'. What did I do wrong?
You've got a bug in your code, simple as that. And without being able to see your code and the actual error message, we've no way of knowing what it is.
That compiler error means that whatever symbol it is talking about (which will be there with the error, and will be a variable or method or class name usually) is not something you have given it. This is often a typo, like not using correct capitalisation (Java is case sensitive).
Look at the mehod and where it is called. you will find what's causing error. For example you might have done like this :
OrCode:public void myNumber(){
// code here
}
mynumber(); // you get error
if still error exist then post your code and error msg.Code:public void myNumber(int i){
// code here
}
myNumber(); // you get error
-regards
There was no typo. The code for the method (in a different file) is:
and for the calling, it's:Code:public String method(String string, int int) {
and the error is:Code:System.println(method("string", 42));
If you need more, just ask, I don't know how much of the code you want. The method files is 150+ lines.Quote:
cannot find symbol
symbol : method method(java.lang.String,int)
The code should be:Disregarding that your example is far away from being compilable.Code:
public class A{
public String method(String string, int anInt) {
return "Blah!";
}
public class B{
public void something(){
A aObject = new A();
System.out.println(aObject.method("string", 42));
}
}
The only part I don't have is the A aObject = new A();
What's that for?
Oh dear lord. Back to the basic tutorials for you.
Trail: Learning the Java Language (The Java™ Tutorials)
Ohh, what should the aObject be then? It uses the arguments in the method(), not an Object.
If the method you are calling is in another class, and is not static, you need to instantiate a new object and then use that to call the method.
Alternatively, you can make the method "static", which means that you can call it without instantiating an instance of the class...Code:MyClass myObject = new MyClass ();
System.out.println (myObject.method ("string", 42));
Code:public static String method (String string, int int) {
// code here
}
Hope this helps.Code:System.out.println (MyObject.method ("string", 42);
Berkeleybross
Wow reading a complete tutorial in three minutes, that must be some kind of record. ;)
@Berkeleybross Thanks, that helped, I didn't realise that I needed to have a caller object.
@PhHein Thanks anyway, I never heard of that kind of object. btw I really like your picture.
Also, follow up question, can I call a method from a project that doesn't have that class file in it? If so, how?
You really need to go through the tutorials.
You shouldn't be focussing on methods. You should be thinking in terms of classes and objects. The methods are simply the things you call on an object.
"that kind of object"? PhHein is not talking about anything special, just basic Java programming, nothing more. If you don't understand this, then you really need to study the basics before trying to do anything else.
What's a "project"? If you call a method, you need to import the class if that's what you mean. Again, all of this is explained in introductory text books and tutorials.Quote:
Also, follow up question, can I call a method from a project that doesn't have that class file in it? If so, how?