Results 1 to 2 of 2
  1. #1
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default How would I declare the variable numbers as global?

    Hi, How would I declare the variable numbers as global? I want to be able to access the same variable from any method so that the value of the variable is the same.
    Java Code:
    import TerminalIO.KeyboardReader;
    
    public class SortAndDestroy{
    	
    	public static void main (String[] args){
    		int[] numbers = {42,37,5,33,6};
    		KeyboardReader reader = new KeyboardReader();
    		int choice;
    		
    		for(int i = 0; i < Names.numbers.length; i++){
    			System.out.print(numbers[i] + "  ");
    		}
    		
    		choice = reader.readInt("\nSelection[1] or Bubble[2] Sort?  ");
    		
    		if (choice == 1)
    			selectionSort(numbers);
    		else if (choice == 2)
    			bubbleSort(numbers);
    		else
    			System.out.println("That is not a choice.");
    	}
    	
    	public static void selectionSort(int[] numbers){
    		for(int i = 0; i < numbers.length; i++){
    			int minIndex = findMinimum(numbers, i);
    			if(minIndex != 1)
    				swap(numbers, i, minIndex);
    			
    			System.out.print(numbers[i] + "  ");
    		}
    	}
    	
    	public static int findMinimum(int[] numbers, int first){
    		int minIndex = first;
    		
    		for(int i = first + 1; i < numbers.length; i++)
    			if(numbers[i] < numbers[minIndex])
    				minIndex = i;
    			
    			return minIndex;
    	}
    	
    	public static void swap(int[] numbers, int x, int y){
    		int temp = numbers[x];
    		numbers[x] = numbers[y];
    		numbers[y] = temp;
    	}
    	
    	public static void bubbleSort(int[] numbers){
    		int k = 0;
    		boolean exchangeMade = true;
    		
    		while((k < numbers.length - 1) && exchangeMade){
    			exchangeMade = false;
    			k++;
    			for(int j = 0; j < numbers.length - k; j++)
    				if(numbers[j] > numbers[j + 1]){
    					swap(numbers,j,j + 1);
    					exchangeMade = true;
    				}
    		}
    	}
    }
    Thanks.

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    U could put the variable inside the class definiton, meaning, under the code and outside of any method declaration, example
    Java Code:
    public class SortAndDestroy{
          int global;
          public void static main(String[] args)
          {    ....and so on....}
    If u want the value remains the same for every instance u created, u could use the keyword static in front of the variable definition

Similar Threads

  1. Global constants
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 10:06 AM
  2. Where is it best to declare swing components?
    By MacNstuff in forum AWT / Swing
    Replies: 1
    Last Post: 02-06-2008, 01:59 AM
  3. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:11 AM
  4. Replies: 0
    Last Post: 09-28-2007, 01:47 PM
  5. save global data in servlets
    By Marty in forum Java Servlet
    Replies: 1
    Last Post: 05-31-2007, 07:38 PM

Posting Permissions

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