Could you please show me a very small program with the explanation how the data can be passed between classes.
I said very small program (as small as you can) because I just started in Java and if the program is big I'll get lost !!!
I'm a bit confused in this part (sending data from one class to another and via versa )
I don't know why I feel Java is an ambiguous programming languag

6 Years
Discussion Span
Last Post by kramerd

what have you seen so far, and, most important, how do you think you can pass data through different classes?


Small example
I have two classes A and B
In class A I add two numbers (x+y)
But instead of print the result on the screen by class A
Class A send the result to class B and print the result on the screen by class B
This a small example with the explanation will be so good for me.
At least I get a brief Idea to start with
Thank you in advance


Well, try for yourself, show us what you have done so far code-wise, and we will correct and guide you.


Thank you for your replay
If you read my first request you will know that this is not homework, because I said any program
I know that if I go to Google I’ll find plenty of programs like this but I know that I’ll not get any benefit from them
Because I want to know the Idea to pass the data between classes
thank you


You said in your first post that Java is an ambiguous language. This is not true. Ambiguous means there is more than one way to interpret it. This is a feature of natural languages, but is very bad in programming languages. However, what Java is, is very general purpose, which means there are many ways to accomplish a given task. This might be what you meant.

Here's one way to accomplish your little example.

// this class goes in a file called A.java
public class A {
   private static final int x = 10;
   private static final int y = 5;

   // method to get the value of x
   public static int getX() {
      return x;
   // method to get the value of y
   public static int getY() {
      return y;

// this class goes in a file called B.java
public class B {
   public static void main(String args[]) {
      // get the values of x & y from class A
      int x = A.getX();
      int y = A.getY();
      // print them out
      System.out.println("x = " + x + ", y = " + y);

Thank you kramerd for your simple understandable example
In my first post I said I feel but reality will not be built on my feeling because I just started with java and I can’t judge java in the first few days of starting.
The matter is I just started and I found Java is not easy to start with comparing with some other programming languages e.g. Vb.net
Any way I started now and I hope that I find it easy in near future
and I'll appreciate that if you give me any advice that can help me in my starting days

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.