473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Returning a value across classes

23
I have a class named collect.java that looks like this:
Expand|Select|Wrap|Line Numbers
  1. public class collect {
  2.     public int returnX() {
  3.         int x=5;
  4.         return x;
  5.     }
  6. }
  7.  
I also have a main class-
Expand|Select|Wrap|Line Numbers
  1. public class testcollect {
  2.     public static void main(String[] args) {
  3.         collect collector = new collect();
  4.         collector.returnX();
  5.         System.out.println(x);
  6.     }
  7. }
  8.  
I was expecting the testcollect class to simply print out 5. Yet I get the error message "cannot find variable x". Please help.
Oct 20 '07 #1
3 1051
Ganon11
3,652 Expert 2GB
Read exactly what the message says. There is no variable x in your main() program. The variable x is not returned by your function - only its value. In order to catch that value, you need to store it in your own variable. If you change your main to:

Expand|Select|Wrap|Line Numbers
  1. //...
  2. int x = collector.returnX();
  3. System.out.println(x);
  4. //...
it will work as expected.
Oct 20 '07 #2
anon538
23
Thank you! I feel so stupid for not thinking of that.
Oct 21 '07 #3
Ganon11
3,652 Expert 2GB
You're welcome. Functions returning values is a fundamental concept in programming, so make sure you understand it well (and understand functions that don't return values as well).
Oct 21 '07 #4

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

Similar topics

12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
3
by: Sean Tynan | last post by:
I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution. e.g. say I have a method (business...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
2
by: FutureShock | last post by:
I am using a registration class to process a registration form and need some opinions on returning error messages. I am self referring the page on submit. I would like to send each form field...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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,...

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.