0

I have 4 classes, Main, class A, class B, class C.
I initiated a variable in Main, outside a loop_1 : int x=0 and then passed it into class A that uses a loop_2, and then from there straight into class B which is a part of that loop and right into class C.

I intended that after class A would finish the loop I'll get System.out.Println with X.

The problem is that I get x=0 every time. I have a checkUp System.out.println in class C and I see it grow there, But as I try to print x from class class A I always get x=0.

Note: I have

Main Class

public class SocketMain
   {

public void mainMethod{
 int x=0;
 for (int edition = 0 ; edition<29 ; edition++){
  classA.Method1(x);

 x=0;
  }
}

class A (SelfNote : // InstrumentPage)

 public class classA 
{

public static void Method1(int x)

{
for (int loopCounter = 0 ; loopCounter <29 ; loopCounter ++){
  classB.Method2(x);


 }


if (x=0)
System.outprintln("FirstIf" +x);

if (x>0)
System.out.println ("SecondIf" +x);
    }

Class B (SelfNote : //TestClockOfInstrument)

 public class classb
          {
public static void Method2(int x)
{

    classC.Method3(x);



}

Class C (SelfNote : //SocketChanges)

 public class classc
{
public static void Method3 (int x)
{
 if (someThingHappens == 1)
  x++;
 System.out.println ("Check X" +x);

}

dima edunov
  • 59
  • 1
  • 9
  • 1
    @BackSlash I don't believe this is an exact duplicate of that one. A person asking this question is unlikely to understand the significance of the concepts explained in that other question. So marking this question as a duplicate is more likely to confuse the OP than to help them. That's my opinion anyway. – Dawood ibn Kareem Mar 13 '18 at 11:27
  • 1
    Maybe you should return the updated value, instead of trying to change the value passed in. – matt Mar 13 '18 at 11:30
  • @BackSlash Its a project I'm working on, that has 4 classes. I deleted all the rest and edited it for nearly 45 minutes so it would be easy to understand. I don't understand why did you mark it as a duplicate. I just need so advise – dima edunov Mar 13 '18 at 11:36
  • 1
    @dimaedunov because you are passing a value in and expecting it to change. If you read and understand the answer, you'll see why your current solution doesn't work. Your method essentially receives a new int that has the same value. So changing it, only changes it locally and not the original. – matt Mar 13 '18 at 11:39
  • @matt , Can you be more specific. How do I return it straight into classA? – dima edunov Mar 13 '18 at 11:40
  • I have fixed some class / Methods in the code. It was wrong. Sorry – dima edunov Mar 13 '18 at 11:53
  • replace void with int. `int method3(int x){ if(condition) return x+1; else return x;}` then `int method2(int x){ return C.method3(x);}` – matt Mar 13 '18 at 11:55

0 Answers0