Siemhong
Siemhong

Reputation: 391

Can not reference class in java

class MyClass {
   int value;
}
public MyClass test(){
   MyClass mc;
   myMethod(mc, 100);
   if (mc!=null) {
      mc = mc.value;
      return mc;
   }
   return mc;
}
public int myMethod(MyClass mc, int a) {
   mc.value = a + 10;
   return mc;
}

I want to make the mc as a reference. If objective-c is similar to this:

public MyClass test(){
   MyClass *mc;
   myMethod(&mc, 100);

}

Please help me. Any answer is appreciate. Thank you in advanced.

Upvotes: 1

Views: 88

Answers (1)

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

In java we use reference variables instead of pointers. See this and this

class MyClass 
{
   int value;

/*All methods have to belong to a class*/

public MyClass test()
{
 /*MyClass mc; you don't need to pass this to assign value the value will be 
 assigned to the data members of the object(reference variable) which calls the
 function*/       

MyClass mc = myMethod(100);
return mc;
}

public MyClass myMethod(int a) 
{
   value = a + 10;
   return this; //This 'this' will return the current object
}
}

Upvotes: 1

Related Questions