Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: "Static method cannot hide instance method from implemented Interface"

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Static method cannot hide instance method from implemented Interface"

    I'm working on an assignment for school and am stumped. I should mention that the operations of static declarations still elude me a bit...

     
    import java.util.Scanner;
     
    public class SortedListArray implements SortedListInterface
    {
    	public static void main(String[] args)
    	{
    		int i=0;
    		int[] userArray = new int[i];
    		int noOfInputs = 0;
    		int submission = 0;
    		int n = 1;
    		int inc = 0;
     
    		Scanner kybd = new Scanner(System.in);
     
    		System.out.println("Please enter the number of integers you will put into the array: ");
    		noOfInputs = kybd.nextInt();  //holds the user-defined no. of integers to be held within the array
     
    		for(i = 0; i < noOfInputs; i++)
    		{
    			kybd.nextLine();
    			System.out.println("Please enter integer" + n);
    			submission = kybd.nextInt();
    			SortedListArray.process(userArray, submission, inc);
     
    			n++;
    		}
     
    		System.out.println(SortedListArray.toString(userArray, noOfInputs));
     
    		SortedListArray.sort(userArray, noOfInputs);
     
    		System.out.println ("Here are the newly sorted entries:\n");
     
    		System.out.println(SortedListArray.toString(userArray, noOfInputs));
     
    	}
     
    	public static void sort(int[] intArray, int inputs) 
    	{
    	//  compares an index value at the beginning of the array to the next index value and switches their order
    	// from smallest to largest in an ascending manner
    	// this is continued for the entire array
    		int temp;
    		for(int i = 0; i < inputs; i++)
    		{
    			for(int j = i + 1; j < inputs; j++)
     
    			if(intArray[i] > intArray[j])
    			{
    				temp = intArray[i];
    				intArray[i] = intArray[j];
    				intArray[j] = temp;	
    			}
    		}
    	}
     
    	public static String toString(int[] intArray, int inputs)
    	{
    	//returns a nicely formatted String value of all of the indices and the values contained within
    		String stringFormat = null;
     
    		System.out.println ("Current contents of the array are: \n\n");
     
    		for (int i = 0; i < inputs; i++)
    		{
    			stringFormat =(stringFormat + "Index " + i + ": " + intArray[i] + "/n");
    		}
    		return stringFormat;
    	}
     
    	public static void process(int[] intArray, int submission, int num)
    	{
    	//processes the user input and moves them into the array indices
     
    		intArray[num] = submission;
    		num++;
    	}
     
    }

    And the implemented interface...

     
     
    public interface SortedListInterface {
     
    	public void sort(int[] intArray, int inputs);
    	// Pre condition: Passed argument is an integer array
    	// method reorganizes the contents of the array from smallest to largest
     
    	public String toString (int[] intArray, int inputs);
    	// pre conditions: Passed arguments must be a valid int array and int variable
    	// returns a well-formatted print out of the current contents of the array
     
    	public void process(int[] intArray, int inputs, int num);
    	//pre conditions: Passed arguments must be a valid int array and int variable
    	//processes the user input and moves them into the array indices
     
     
    }

    So yeah, I get the error message that is shown in the title highlighted in all of the methods in the class file. If I take the static declaration away from the methods then the error will go away, but a new one pops up over any instance in which the method is called. It says, "Cannot make static reference to non-static method xxx"

    Please school me on the meaning of static in regards to implementation. Or just in general.

    Thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: "Static method cannot hide instance method from implemented Interface"

    When a class implements an interface it is bound by a contract to provide an implementation of all the abstract methods in the interface. Your SortedListInterface has non-static methods and yet in your SortedListArray they are all static. That does not meet the contract.

    In simplest terms static means it belongs to the class and not an instance of that class. For example if you had a Person class and a static varaiable name then every Person in the world would have the same name. That doesn't make sense. So you make name an instance variable so each instance/object of the Person class has its own name.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Static method cannot hide instance method from implemented Interface"

    Ok, I understand and agree with all of this. My professor wants me to implement a simple sort list ADT. I figured implementing an interface would be a good way to go about it.

    I know you can't create static methods in interfaces themselves since defining a static method would mean that that method needs a body, which an interface doesn't support. How do I go about fixing this? I try to take the static declaration off and then another error occurs whenever I call the method that says it must be static. Should I just never implement an interface when I know I will be dealing with static methods?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: "Static method cannot hide instance method from implemented Interface"

    OK removing the static keyword is the correct action. Just because it raises/reveals other errors doesn't mean that it was wrong. As soon as you make those methods non-static the only way to access them is to create an instance of your class and then you can call the methods on that object.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    9
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: "Static method cannot hide instance method from implemented Interface"

    Oh man, I can't believe I missed that. I didn't even have constructors made yet!

    Thanks!

Similar Threads

  1. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  2. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  3. "showMessageDialog" method in swing package
    By Delmi in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 02:52 PM
  4. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM