473,674 Members | 4,828 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 84001
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
10048
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
3056
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 another class. Well, let me just type up a little example: class MainClass { $obj = null;
2
11277
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
10859
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
1684
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 because in the constructor for class B in the user control we have a call to
6
16606
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
3809
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
2137
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
3889
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
7559
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
8440
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8964
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8860
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8667
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7498
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5744
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4259
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2115
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.