469,326 Members | 1,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

How to call a method in one class to another class

Expand|Select|Wrap|Line Numbers
  1. /* first.java */
  2. package test;
  3. public class first
  4. {
  5.  public static void display()
  6. {
  7.  System.out.println(" welcome to java");
  8. }
  9. }
  10.  
  11. /*second.java */
  12. package test;
  13. public class second
  14. {
  15.  void check()
  16.    {
  17.     first.display();
  18.   }
  19. }
  20.  
  21.  

I have stored above code in test folder. first.java is compiled successfully. When I am compiling second.java , I got the following error message
"cannot resolve the symbol variable first".

How to correct this error?
Aug 31 '07 #1
18 83154
kreagan
153 100+
Expand|Select|Wrap|Line Numbers
  1. /* first.java */
  2. package test;
  3. public class first
  4. {
  5.  public static void display()
  6. {
  7.  System.out.println(" welcome to java");
  8. }
  9. }
  10.  
  11. /*second.java */
  12. package test;
  13. public class second
  14. {
  15.  void check()
  16.    {
  17.     first.display();
  18.   }
  19. }
  20.  
  21.  

I have stored above code in test folder. first.java is compiled successfully. When I am compiling second.java , I got the following error message
"cannot resolve the symbol variable first".

How to correct this error?
I honestly don't see anything wrong. One thing you can check is check for spelling. Remember, Java is case sensitive. Also, I don't know why you are making the methods static. It's better to create a object which performs the method.

Expand|Select|Wrap|Line Numbers
  1. /* first.java */
  2. package test;
  3. public class first{
  4.      public void display(){
  5.               System.out.println(" welcome to java");
  6.      }
  7. }
  8.  
  9. /*second.java */
  10. package test;
  11. public class second {
  12.  void check(){
  13.     first myFirstJava = new first();  //create an object of type first
  14.     myFirstJava.display();    //use object to display. 
  15.   }
  16. }
Hopefully this helps.
Aug 31 '07 #2
kreagan
153 100+
You have not declared/created an instance of the first class in the second. Try declaring a 'first' class in the constructor.
Does he need to? The method is static so can't it be called by the class instead of an object of that class. Take a look at Math. To call Math functions, you don't need to declare an instance. Is that because the methods are static or because the class is final?
Aug 31 '07 #3
sicarie
4,677 Expert Mod 4TB
Does he need to? The method is static so can't it be called by the class instead of an object of that class. Take a look at Math. To call Math functions, you don't need to declare an instance. Is that because the methods are static or because the class is final?
I'm switching between too many documents this morning and didn't read it all (hence my deletion of a useless post ;).

::sigh:: I was trying to cut back, but I definitely need coffee this morning...
Aug 31 '07 #4
I have created instance of first class in second using the following code
Expand|Select|Wrap|Line Numbers
  1. first fa=new first();
  2.  
Again I got the error (Cannot resolve symbol class first . Location: class test.check)
Aug 31 '07 #5
kreagan
153 100+
I'm switching between too many documents this morning and didn't read it all (hence my deletion of a useless post ;).

::sigh:: I was trying to cut back, but I definitely need coffee this morning...
excuse me? Maybe you need another cup.
Aug 31 '07 #6
sicarie
4,677 Expert Mod 4TB
excuse me? Maybe you need another cup.
Yep, just picked one up. Sorry about that.
Aug 31 '07 #7
kreagan
153 100+
I have created instance of first class in second using the following code
Expand|Select|Wrap|Line Numbers
  1. first fa=new first();
  2.  
Again I got the error (Cannot resolve symbol class first . Location: class test.check)
the second test (named test) can't see the first class. Are you sure you spelt the name correctly? Also, are these files in the same folder? Try making an object in the first class which calls that function.

Also, what IDE are you using?
Aug 31 '07 #8
kreagan
153 100+
Yep, just picked one up. Sorry about that.
So this is kind of off topic. Why don't you need to create a Math object when using Math methods? I always thought it was because the methods were static.
Aug 31 '07 #9
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. /* first.java */
  2. package test;
  3. public class first
  4. {
  5.  public static void display()
  6. {
  7.  System.out.println(" welcome to java");
  8. }
  9. }
  10.  
  11. /*second.java */
  12. package test;
  13. public class second
  14. {
  15.  void check()
  16.    {
  17.     first.display();
  18.   }
  19. }
  20.  
  21.  

I have stored above code in test folder. first.java is compiled successfully. When I am compiling second.java , I got the following error message
"cannot resolve the symbol variable first".

How to correct this error?
When you compile your second source file the compiler has know where the
other (the first) class is stored; it uses the classpath for that purpose. You can
set the classpath value on the compiler's command line:

javac -cp /path/where/first/is/stored second.java

kind regards,

Jos
Aug 31 '07 #10
sicarie
4,677 Expert Mod 4TB
So this is kind of off topic. Why don't you need to create a Math object when using Math methods? I always thought it was because the methods were static.
Okay, I'm not entirely sure of the concepts behind this (I just left my Java programming job, and have since immersed myself in assembler and Ruby, so this is rapidly slipping away), but the Math lib works because you are directly calling it (Math.pow() or whatever). There is a way you can set up classes so that you don't actually need to instantiate them to use the methods they invoke, but I can't remember what it is off the top of my head. brb - going to research on Google.

Maybe another Expert or Mod can interject while I'm searching Google desperately trying to get caffeine into my system.
Aug 31 '07 #11
JosAH
11,448 Expert 8TB
Maybe another Expert or Mod can interject while I'm searching Google desperately trying to get caffeine into my system.
Read my previous reply (#10).

kind regards,

Jos
Aug 31 '07 #12
kreagan
153 100+
Okay, I'm not entirely sure of the concepts behind this (I just left my Java programming job, and have since immersed myself in assembler and Ruby, so this is rapidly slipping away), but the Math lib works because you are directly calling it (Math.pow() or whatever). There is a way you can set up classes so that you don't actually need to instantiate them to use the methods they invoke, but I can't remember what it is off the top of my head. brb - going to research on Google.

Maybe another Expert or Mod can interject while I'm searching Google desperately trying to get caffeine into my system.
I looked into this too. (I don't have a java compiler). The reason why you don't need to create an object of type Math is because the methods are static. Therefore, Beno's line

Expand|Select|Wrap|Line Numbers
  1. first.display();
should be valid.

http://www.javaworld.com/javaworld/j...1-mrhappy.html
Aug 31 '07 #13
sicarie
4,677 Expert Mod 4TB
I looked into this too. (I don't have a java compiler). The reason why you don't need to create an object of type Math is because the methods are static. Therefore, Beno's line

Expand|Select|Wrap|Line Numbers
  1. first.display()
should be valid.

http://www.javaworld.com/javaworld/javaqa/2001-11/03-qa-1121-mrhappy.html
Yeah, I was getting to there. I'm still thinking that Math is actually just an interface, but haven't been able to confirm that.
Aug 31 '07 #14
kreagan
153 100+
Yeah, I was getting to there. I'm still thinking that Math is actually just an interface, but haven't been able to confirm that.
lol. Java.sun.com is your friend! Math is "public final class". (which is cool because I never knew they existed.)

Math Class
Aug 31 '07 #15
sicarie
4,677 Expert Mod 4TB
lol. Java.sun.com is your friend! Math is "public final class". (which is cool because I never knew they existed.)

Math Class
Yeah, ever tried to create a class that you cannot copy/inherit? public final classes are fun, but most people also forget to overload the copy constructor...

There we go:
By default many of the Math methods simply call the equivalent method in StrictMath for their implementation
Aug 31 '07 #16
JosAH
11,448 Expert 8TB
Instead of complicating matters even further: it was a compilation problem: the
compiler couldn't find class 'first' (bad name by the way for a class).

I already answered the OP in reply #10. It's another classpath issue again.

kind regards,

Jos
Aug 31 '07 #17
praveen2gupta
201 100+
Expand|Select|Wrap|Line Numbers
  1. /* first.java */
  2. package test;
  3. public class first
  4. {
  5.  public static void display()
  6. {
  7.  System.out.println(" welcome to java");
  8. }
  9. }
  10.  
  11. /*second.java */
  12. package test;
  13. public class second
  14. {
  15.  void check()
  16.    {
  17.     first.display();
  18.   }
  19. }
  20.  
  21.  

I have stored above code in test folder. first.java is compiled successfully. When I am compiling second.java , I got the following error message
"cannot resolve the symbol variable first".

How to correct this error?
Hi
you are trying to access the method without creating the object of the class.
create the object of class first and then access the method associated with it.
Sep 1 '07 #18
JosAH
11,448 Expert 8TB
Hi
you are trying to access the method without creating the object of the class.
create the object of class first and then access the method associated with it.
No that is not needed; read the thread and you'll notice that it's (just) a classpath
issue.

kind regards,

Jos
Sep 1 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by christopher vogt | last post: by
6 posts views Thread by Steph | last post: by
1 post views Thread by cllee | last post: by
3 posts views Thread by asahli | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.