3 Replies - 482 Views - Last Post: 05 October 2012 - 01:01 PM Rate Topic: -----

#1 byebyemoney  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-October 12

Can static variables not be changed inside a static method?

Posted 05 October 2012 - 08:17 AM

I have a program that performs an algorithm that sorts an array's elements into ascending order. I NEED to have a counter that records how many times it compares (checks an element in the array) and how many times it exchanges elements (swaps elements to put them in ascending order). When I call upon these counters in a different class after performing the method I keep just getting 0?

Someone please help, here is class, variables and method that is fed in an array.

 
public class exchangeSortPart {
			
				static int counterC = 0;   // counts the comparisons
				
				static int counterE = 0;   // Counts exchanges
				
				public static int[] exchangeSort(int[] list){        // method for insertion sort
				
							
								
								int temp;  // this is used to keep a copy of the element being switched
								    
									 for (int i = 0; i < list.length-1;i++) {    // for loop for running through array, the .length-1 because of the next for loop
											
											
											
								          for (int j = i + 1; j < list.length; j ++ )     // compares next element in array
								
								          {
												counterC++;   // this counts a comparison taking place
								               if( list[i] > list[j] )         // if the value on the left is greater than the value on the right, switch that bitch!
								               {
								                       temp = list[i];     // make a copy of the left element to be switched 								                       
											            list[i] = list[j];
								                       list[j] = temp;
												         counterE++;     // this counts that an exchange took place
								                }           
								          }
								     }
			
							
						
						return list;    // return list array
						

				}  // end of exchangeSort method
				
				
		}		// end of exchangeSortPart Class




Is This A Good Question/Topic? 0
  • +

Replies To: Can static variables not be changed inside a static method?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: Can static variables not be changed inside a static method?

Posted 05 October 2012 - 08:58 AM

I ran your code

public class ExchangeSortPart {
	static int counterC = 0; // counts the comparisons
	static int counterE = 0; // Counts exchanges

	public static int[] exchangeSort(int[] list) { // method for insertion sort

		int temp; // this is used to keep a copy of the element being switched

		for (int i = 0; i < list.length - 1; i++) { // for loop for running through array, the .length-1 because of the next for loop
			for (int j = i + 1; j < list.length; j++) // compares next element in array
			{
				counterC++; // this counts a comparison taking place
				
				if (list[i] > list[j]) // if the value on the left is greater than the value on the right, switch that bitch!
				{
					temp = list[i]; // make a copy of the left element to be switched 								                       
					list[i] = list[j];
					list[j] = temp;
					counterE++; // this counts that an exchange took place
				}
			}
		}

		return list; // return list array

	} // end of exchangeSort method
	
	public static void main(String[] args) {
		int[] array = { 1, 10, 5, 11, 12 };
		ExchangeSortPart.exchangeSort(array);
		System.out.println(ExchangeSortPart.counterC);
		System.out.println(ExchangeSortPart.counterE);
	}

} // end of exchangeSortPart Class





And got the results
10
1



I don't see any zeros :)

Please follow java syntax and start your class names with a capital letter
Was This Post Helpful? 0
  • +
  • -

#3 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Can static variables not be changed inside a static method?

Posted 05 October 2012 - 09:17 AM

Just a few notes on your code. Always remember every word in a class name starts with a upper case letter.
Instead of exchangeSortPart make it ExchangeSortPart. This just makes it easier to find classes.

To answer your question. The only kind of global variables a static method can change are static variables.
Why is this? When you make a global variable in a class this variable is said to belong to the instance of the class i.e. the object. Each object has it's own version of that variable. This is why you do not need a instance of that class to use the variable. You can just use the class name followed by a dot.

Now when you make something static you make it belong to the class NOT the object. Every object of that class will share this variable, so if one object changes that variable it changes for ALL the objects.

So to be sure that this variable changes for every object. The only thing that can change this variable would have to be something else that belongs to the class and not the object. i.e. static methods.

Your class should not be static at all unless you are wanting to count how many times comapres/exchanges no matter how many times you run is. What I mean by this is if I have List A and List B if I run your program with List A then List B the counters would count List A AND List B.

public class StaticExample {
	public static void main(String[] args) {
		int[] a = {5,3,25,64,3};
		int[] b = {5,3,25,64,3};
		
		a = ExchangeSortPart.exchangeSort(a);
		System.out.printf("CountE = %d\nCountC = %d",ExchangeSortPart.counterE, ExchangeSortPart.counterC);
		b = ExchangeSortPart.exchangeSort(B)/>;
		System.out.printf("\nCountE = %d\nCountC = %d",ExchangeSortPart.counterE, ExchangeSortPart.counterC);
	}
}

//prints out CountE = 4 CountC = 10
//Then prints out CountE = 8 CountC = 20



However I ran your code and everything came out fine.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: Can static variables not be changed inside a static method?

Posted 05 October 2012 - 01:01 PM

What you have there is a class, but with all that messy static, it can't be an object. Also, this is a selection, not an insertion, sort. If it was an object, you could generalize your counters via inheritance. In any case, initializing those counters on the sort call would help.

e.g.
abstract class SortCounter {
	private int counterC, counterE;
	public SortCounter() { initCounters(); }
	protected void initCounters() { counterC = counterE = 0; }
	protected boolean compare(int[] list, int i, int j) {
		this.counterC++;
		return list[i] > list[j];
	}
	protected void swap(int[] list, int i, int j) {
		int temp = list[i];                    
		list[i] = list[j];
		list[j] = temp;
		counterE++;
	}
	protected abstract void sortImpl(int[] list);
	public void sort(int[] list) { initCounters(); sortImpl(list); }
	public int getCompares() { return this.counterC; }
	public int getExchanges() { return this.counterE; }
}
class SelectionSortCounter extends SortCounter {
	protected void sortImpl(int[] list) {
		for (int i = 0; i < list.length-1; i++) {
			for (int j = i + 1; j < list.length; j++) {
				if(compare(list, i, j)) { swap(list, i, j); }
			}
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1