Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2007, 05:56 PM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
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.
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 04:17 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
U could put the variable inside the class definiton, meaning, under the code and outside of any method declaration, example
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Global constants Java Tip Java Tips 0 02-17-2008 11:06 AM
Where is it best to declare swing components? MacNstuff AWT / Swing 1 02-06-2008 02:59 AM
Declaring global variables eva New To Java 3 12-23-2007 02:11 AM
Defining global session scope in spring framework JavaBean Java Tips 0 09-28-2007 02:47 PM
save global data in servlets Marty Java Servlet 1 05-31-2007 08:38 PM


All times are GMT +3. The time now is 11:15 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org