Results 1 to 2 of 2
  1. #1
    Warren is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default variables-methods

    how can i use, in a method, the variable (not only the type and the name but its corresponding value also) i have used in a previous method? (both/all methods being in the same class).

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    All variables in Java are passed by value. So if you pass a variable to a method that changes the value, the variable still exists in its original state.

    Java Code:
    public class Thingie{
    
    	public Thingie(){
    	}
    	
    	public int changeValue(int i){
    		return i + 7;
    	}
    	
    	public static void main(String[] args){
    		Thingie foo = new Thingie();
    		int start = 10;
    		
    		//this will print 10
    		System.out.println(start);
    		
    		//this will print 17
    		System.out.println(foo.changeValue(start));
    		
    		//this will still print 10
    		System.out.println(start);
    	
    	}
    
    
    }
    Hopefully this helps.

Similar Threads

  1. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 PM
  2. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:08 AM
  3. Variables
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 01:44 PM
  4. JSP - session variables
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-02-2007, 10:22 PM
  5. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 10:52 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
  •