473,585 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to call a method in one class to another class

28 New Member
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 83992
kreagan
153 New Member
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 New Member
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 Recognized Expert Moderator Specialist
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
benoypaul
28 New Member
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 New Member
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 Recognized Expert Moderator Specialist
excuse me? Maybe you need another cup.
Yep, just picked one up. Sorry about that.
Aug 31 '07 #7
kreagan
153 New Member
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 New Member
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 Recognized Expert MVP
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

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

Similar topics

5
10043
by: christopher vogt | last post by:
Hi, i'm wondering if there is something like $this-> to call a method inside another method of the same class without using the classname in front. I actually use class TEST { function func1()
2
3050
by: Lüpher Cypher | last post by:
Ok, here's what I'm trying to do. I have a class. Inside the class I have a method that reads a text file. In the text file I have a class name and a file name where that class is defined. Now, I need to instantiate an object of that class, the problem being that at the time I find out the class name and the file name, I'm inside a method of...
2
11274
by: Jan | last post by:
Here's the code with the problem: class Startup { public static string Sym; public static string PrevDate; public static int Highest = new int; // ______________
6
10834
by: Tee | last post by:
Hi, Can anyone tell me if it's possible to pass a method to another class? Example of what I would like to do: class1: public void MyMethod() {
4
1679
by: Tony Johansson | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. Here we have a class called B One project that build a class library dll. Here we have a class called C We have one dependency and that is from the user control to the class library...
6
16591
by: Steph | last post by:
hello, i have two application, Application 1 : with a method : public int math_add(int a, int b) { return a+b;} and a another application 2 : Process NewProcessList2 = Process.GetProcessesByName("WindowsApplication1TEST"); foreach (Process TempProcess in NewProcessList2)
1
3805
by: cllee | last post by:
hello everyone, i may need urs help. i am facing problem now. i am using java applets. then, i have a method with a very long coding..So, i just open a new class. now, how do i call the method if in another file? can anyone help me please?Thanks .. u all ..can send the solution to <posting email is gainst site rules: removed by Admin >
3
2134
by: Pawel_Iks | last post by:
I have following class definition (A.h file): class A { public: int N; int count1(int n) {n++;} int count2(int n) {n+=5;} int otherFun(int n, int (*fun)(int)); }
5
3887
by: nickyeng | last post by:
i have 2 projects but i am not sure how to integrate them. from 1st project, i have to call some function in 2nd project. what source code should i add into my 1st project ? use "import" ? but import is used within the same project but not with other project , right ? so what should i do ?
3
7556
by: asahli | last post by:
Hi guys, i have made two java files as followin: 1st file: import java.io.*; public class Reader { private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); public static void main ( String args ) throws IOException
0
7900
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8204
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5382
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3828
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3853
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1167
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.