C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

How to pass Reference to a method?

0 votes
81 views
asked by smith Geek (15,340 points)

1 Answer

0 votes
class Point {
  public int x;

  public int y;
}

public class MainClass {
  public static void increment(int x) {
    x++;
  }

  public static void reset(Point point) {
    point.x = 0;
    point.y = 0;
  }

  public static void main(String[] args) {
    int a = 9;
    increment(a);
    System.out.println(a); // prints 9
    Point p = new Point();
    p.x = 400;
    p.y = 600;
    reset(p);
    System.out.println(p.x); // prints 0
  }
}
output
9
0
answered by augustaina Sr Member (4,820 points)

Related questions

0 votes
1 answer 82 views
0 votes
1 answer 159 views
0 votes
1 answer 14 views
14 views asked by smith Geek (15,340 points)
0 votes
1 answer 81 views
0 votes
1 answer 85 views
85 views asked by augustaina Sr Member (4,820 points)
...