-2

After searching and reading a lot of examples about passing values/objects between classes/methods I still don't get it . . . Let's say i have this little code (which is wrong):

public class Main{
    public static void main(String[] args){
        Print print = new Print();
    }
}

//end of Main class

class ClassA{
    int x = 3;
    int y = 5;
    public classA(int a, int b){
        a = x;
        b = y;
    }
}
class ClassB{
    int c;
    public classB(int a, int b){
        c = a * b;
    }
}
class Print{
    int c;
    public Print(int c){
    System.out.println("C is equal to: " + c);
    }
}

This code example doesn't work but i want to understand once and forever how does passing values works in java. Thank you.

Besan Vadim
  • 441
  • 4
  • 8
  • 1
    Possible duplicate of [How to do the equivalent of pass by reference for primitives in Java](http://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java) – Whymarrh Jan 31 '16 at 18:16
  • Your code sample does not compile. Further it is not clear what you are asking. – hotzst Jan 31 '16 at 18:23
  • @hotzst *"Your code sample does not compile."* Maybe that's why OP wrote "which is wrong" and "This code example doesn't work"? ... – Tom Jan 31 '16 at 18:24
  • @Tom in that case it is badly worded as the title says passing values and the post provides two classes that are never used. – hotzst Jan 31 '16 at 18:26
  • @hotzst OP doesn't know how to pass a value to the `Print` constructor, so the title is correct. – Tom Jan 31 '16 at 18:35
  • 1
    Although ClassA and ClassB are not used in the program, maybe the methods `classA` and `classB` should start with a capital C (to become constructors), and in ClassA I think `a=x; b=y;` was intended to be `x=a; y=b;` – FacundoJ Jan 31 '16 at 18:36

4 Answers4

1

Add some int value when you instance Print like this:

  Print print = new Print(445);
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
0

The constructor must have the exactly same name as the simple name of the class (class name without package). Case sensitive. Use public ClassA instead of public classA on line 4.

Variable assigning is in this syntax:

{target_to_assign_to} = {value_to_assign};

You are doing it the opposite way.

Also, you have to pass a value to the Print() constructor, as @Abdelhak pointed out.

SOFe
  • 7,867
  • 4
  • 33
  • 61
0

Print print = new Print();

In above statement your trying to call default constructor. Which becomes unavailable when you define your own constructors. So either define a default constructor your self and pass a value.

Print print = new Print(10);

OR

class Print{
    int c;
    public Print(int c){
    System.out.println("C is equal to: " + c);
    }
    public Print(){
    System.out.println(" Handles : new Print() ");
    }

}
PrakashSharma
  • 386
  • 5
  • 16
0

Firstly you never call ClassA and ClassB (you also have to write the constructor of a class exactly like it; so ClassA instead of classA), so you can basically remove them.

Now about passing values. You actually never pass a value. When creating an object you can pass arguments to the constructor of the class the object belongs to. So when you create a new Print object in your Main class, you have to pass the constructor of the Print class a value.

Print print = new Print(5); //5 is the value you pass

Another thing that is probably confusing for you: You set int c in your Print class, which means it as an attribute. Every Print object you create will have an int which you can access by printObject.c for example you could write

Print print = new Print(5); // Sets your locale variable c for printing it to 5
print.c = 5; // Sets the attribute c to 5

It would be way less confusig, if you would name them differently. Like this:

class Print {
  int myInt;
  public Print(int c){
    System.out.println("C is equal to: " + c);
  }
}

If you actually want to set the attribute myInt (you called it c) to a value you pass to the constructor, the code would look like this:

class Print{
  int myInt;
  public Print(int c){
    this.myInt = c; // Sets the value of the attribute myInt to the value of c
    System.out.println("C is equal to: " + c); // Prints out the value you passed
  }
}