Implement the method of passing parameters by reference in C # syntax in Java _java

Source: Internet
Author: User

In C #, the values are changed when called in the secondary function, and changes are also made in the main function

The REF keyword causes the argument to be passed by reference. The effect is that when control passes back to the calling method, any changes made to the parameter in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword

The Out keyword causes the arguments to be passed by reference. This is similar to the REF keyword, except that ref requires that the variable be initialized before it is passed. To use the out parameter, both the method definition and the calling method must explicitly use the Out keyword.

Java inside unlike C #, Java only Object type parameter is the address, Java base type variables to do parameters, all just pass the value.

The solution is to use the Java class as the parameter type of the function.

The following is an example of what I wrote:

FileName: Byrefvariable.java

--------------------------------------public 
class byrefvariable{public  
 static void Main (String args[]) { 
 obj test=new obj ();  
 System.out.println ("The value of the Test.var that is not passed to the Changeit method by the test as an argument =" +test.var); 
 Changeit (test);//Sit arguments passed to a function!  
 System.out.println ("test as argument passed to Changeit after" +test.var);  
 } 
  
 public static void Changeit (obj a) {  
 a.var=12580  
 } 
} 
 
Class obj//I define a class here. 

The above is a small series for everyone to implement in the Java C # syntax in the reference to pass the parameters of the method of the whole content, hope to help everyone, a lot of support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.