Results 1 to 3 of 3
  1. #1
    LuisSuarez is offline Member
    Join Date
    Apr 2016
    Posts
    8
    Rep Power
    0

    Default Make reference to method

    Java Code:
    public static void main(String [] args){
    
        int array [] = {3,4,8,34,67};
        addfive(array);
    
            for (int y : array){
    
                System.out.println(y);
    
            }
    
    }
    
    
        public static void addfive(int x []) {
    
        //why x.length instead of array.length?
        for (int i = 0; i < x.length ; i++){            
    
            x[i]+=5;
    
        }
    
    
    }
    Could anyone explain the parameter (int x []) in the method? My understanding is that it creates another object named x in the method of addfive, which is referenced to int array [], and it will put int array [0], int array [1]............int array [4] into int array [0], int array [1]...............int array [4] once I call addfive(array). Is my understanding correct?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Make reference to method

    Method addfive expects a reference to an array as parameter. It then performs some operations on that array. After that, the main method continues with the changed numbers.

    x.length is used because you want the length of x. The array "array" is defined in the main method and thus not in the scope of any other method. Array "array" in main and "x" in addfive both point to the same array. It does not create a new object.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Make reference to method

    You are correct except it doesn't really create a new object. The int x[] is simply the type that is expected. So the array reference is passed to x where each array element is bumped by 5.

    Regards,
    Jim
    Last edited by jim829; 04-18-2016 at 04:51 PM. Reason: sigh -- Just call me slow draw McGraw
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 05-26-2015, 04:05 AM
  2. How do I make reference to CartBean?
    By Kareng in forum New To Java
    Replies: 5
    Last Post: 11-16-2012, 10:32 AM
  3. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •