+ Reply to Thread
Results 1 to 3 of 3

Thread: Java Methods

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Java Methods

    Java Methods

    It is hard for me to talk about the structure of methods without talking about classes. A method is like a function that is used to act on data in a class. With that in mind, I will use static methods to explain passing parameters and return values.

    Static Methods

    A static method is a method in a class that you can call without creating an instance of the class. The Math class contains a lot of static methods.

    Example:

    Code:
    Math.round(5.35);
    A static method is called by using the class name followed by the method name.

    If I statically import the Math class then I can use the round method like this:

    Code:
    round(5.35);
    However, I don't like to do this. It makes more sense to me when I know what class contains the method.

    To write a static method you include the static keyword in the method prototype.

    Example:

    Code:
    public static void out(int val) {
    	System.out.println(val);
    }
    The method is declared to be public and static. It doesn't have a return value. To call this method I would just use the name of the class followed by the method name.

    Assume that the above method is defined in the class Test. I would then call the method like this:

    Code:
    Test.out(6);
    The output would be:

    7
    One more thing to note about static methods is that main will always be a static method.


    Passing Parameters by Copy

    When you pass a primitive variable to a method, it will always be passed as a copy. Thus any changes in the methods are not made outside of the method.

    Example:

    Code:
    public static void out(int v) {
    	v++;
    	System.out.println(v);
    }
    
    public static void main(String[] args) {
    	int nVal = 6;
    	out(nVal);
    	System.out.println(nVal);
    }
    The output is:

    7
    6
    Why? When we call the out method the method receives a copy of nVal, and incrementing the variable in the method increments a copy of the variable. The changes are not made to the original variable.

    Passing by reference

    If you want to update a value in a method, you can't pass by copy. However, in Java you are not allowed to pass variables by reference. However, Objects are passed by reference. If you want to update a primitive in the method then you can pass the primitive wrapper instead.

    Java provides wrapper objects for every primitive data type. Ex: the int wrapper is called Integer.

    Try this:

    Code:
    public static void main(String[] args) {
    	Integer v = new Integer(5);
    	out(v);
    	v++;
    	System.out.println(v);
    }
    
    public static void out(Integer v) {
    	v++;
    	System.out.println(v);
    }
    The output of main will be:

    6
    7
    Why? Since the object is passed by reference, when it is updated in the method is outpated in main as well. Arrays are also passed by reference. If you don't want the original array or object to be updated then you need to make a copy either in main before passing or in the method before you do any modification.

    Local variables

    When you create a variable in a method, it is local to that function. This means that once the function returns a value any variable created in the method no longer exists.

    Try this:

    Code:
    public static void out(int v) {
    	int x = 2;
    	v+=x;
    	System.out.println(v);
    }
    
    public static void main(String[] args) {
    	out(6); // outputs 8
    	System.out.println(x); // ERROR
    }
    The output line will throw an error because the x variable doesn't exist in main or as a static variable that is public.

    Returning arrays

    Before you name the function, you indicate the return type of the function. You can return an array by writing the function like this:

    Code:
    public static boolean[] sieve(int n) {
    	boolean[] arbPrimes = new boolean[n+1];
    	
    	for (int i=2;i<n;i++) {
    		if (!arbPrimes[i]) {
    			for (int x=2;x*i<n;x++) {
    				arbPrimes[i*x] = true;
    			}
    		}
    	}
    	return arbPrimes;
    }
    This function will return a reference to a boolean array. So in main you can populate a boolean array like this:

    Code:
    boolean[] primes = sieve(500);
    Returning values

    You can return any data type you want. Similar to the function above, just replace boolean[] with whatever data type you want. The function must return a value. However, it can have multiple return points.

    Code:
    public static int ret(int n) {
    	if (n == 5) {
    		return n*2;
    	}
    }
    This code will not compile because it doesn't return a value. You might say yes it does. No, it doesn't. It only returns a value if n is 5. The function must always return a value.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Java Methods

    Another great tutorial! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Java Methods

    This is a great example of how Java and C++ differ. Java handles all objects using what C++ would call pointers. Java cannot create an object the same way as a primitive, unlike C++. Not saying one's better than the other, but it causes some adjustment when switching languages.

    +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Methods... think I over did it on the last one
    By wolfman in forum General Programming
    Replies: 4
    Last Post: 10-14-2011, 10:59 AM
  2. JAVA - wildcards and generic methods
    By wim DC in forum Java Help
    Replies: 2
    Last Post: 07-22-2010, 10:37 PM
  3. Problem with providing the methods [java, gwt]
    By piotr286 in forum Java Help
    Replies: 1
    Last Post: 07-14-2010, 11:34 AM
  4. Java sort methods
    By ToMegaTherion in forum Java Help
    Replies: 0
    Last Post: 05-21-2010, 12:42 AM
  5. Java similar methods?
    By chili5 in forum Java Help
    Replies: 6
    Last Post: 08-27-2009, 07:12 AM

Tags for this Thread

Bookmarks

Posting Permissions

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