• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non-static method cannot be refrenced from a static context  RSS feed

 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, some background info. I'm trying to implement the interface MySet using arrays. One of the methods in MySet is
boolean isIn(int v). This method searches the set and returns true if 'v' is in the set. I'm implementing MySet with the class MyArraySet, so boolean isIn(int v) is in MyArraySet.


Now, the first thing I tried is putting static in front of isIn in MyArraySet, and I got the error that it can't implement MySet.isIn as the overriding method was static. So I tried putting static in front of MySet.isIn, and I got the error that it can't be static because MySet is an interface.

Does anybody have any idea how I could work around or fix this?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no work around if you want to keep using an interface. Interface methods are meant to be used from an instantiated implementation. So you'll have to create an instance of your MyArraySet before using isIn(int v).
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, that sounds logical.. but I can't make a constructor in MySet, so how would I create an instance of MySet? I've tried using things like:

int [] arrSet = new MySet;
or
MySet arrSet = new int [];

but both times I get errors saying that MySet is abstract and can't be initiated or something to that effect.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MySet mySet = new MyArraySet();

This might be a good primer for you.
 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot

Where I needed to be taking inputs and turning them, into a set, I was turning them into an int array, so I had the whole thing wrong. Okay, this leads me to a new question, I'll ask here and if it gets in-depth enough, then I'll make a new thread, but here goes:

Since I need to implement using arrays, how would I take the mySet that you created, and use arrays to manipulate it? As mentioned, I was just using int arrays previously, so I was using normal array manipulation to change everything around as I desired.
For example, the add method inserts a value into the first position in the array, so I was taking the array I created form their inputs as a global variable so I could refrence it from the MyArraySet class, and then the value that they input would be position 0 in a temp array, and the positions from the original array would be their old position+1.

Basically, I don't know how to use arrays to implement the MySet's if I'm going to be creating MySet's instead of arrays.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Szpik wrote:thanks a lot

Where I needed to be taking inputs and turning them, into a set, I was turning them into an int array, so I had the whole thing wrong. Okay, this leads me to a new question, I'll ask here and if it gets in-depth enough, then I'll make a new thread, but here goes:

Since I need to implement using arrays, how would I take the mySet that you created, and use arrays to manipulate it? As mentioned, I was just using int arrays previously, so I was using normal array manipulation to change everything around as I desired.
For example, the add method inserts a value into the first position in the array, so I was taking the array I created form their inputs as a global variable so I could refrence it from the MyArraySet class, and then the value that they input would be position 0 in a temp array, and the positions from the original array would be their old position+1.

Basically, I don't know how to use arrays to implement the MySet's if I'm going to be creating MySet's instead of arrays.


You use need to use the arrays internally in the MyArraySet class:

 
Derek Szpik
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you So I can start off by making sets with the data read from the user, and then that data gets turned into arrays and manipulated inside MyArraySet, sounds easy enough, and instead of re-writing a lot of code, I can just move it into the appropriate MyArraySet method. Cheers!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!