Reputation: 15
Hello guys I have a question. I have an error when I am trying to pass an array of object to a method My class
public class Object {
private int x1;
public Object(int a ,){
this.x1=a;
}
public class staticMethods{
public static void findMaxPos(Object[] name){
max = name[0]
pos = 0
for( int i=1; i<name.length ; i++){
if ( name[i] >max ){
max = name[i];
pos = i;
}
}
}
public class Example{
public static void main(String[] args) {
Object[] yp = new Object2[3];
yp[0] = new Object(5);
yp[1] = new Object(6);
yp[2] = new Object(8);
findMaxPos(type)// i get an error of the method findMaxPos is undefined for the type Example
}
So sorry for the long post ...
Upvotes: 0
Views: 14169
Reputation: 1
Well, the above solution should work but apart from that, there are a couple of other things that need to be taken into consideration.
Upvotes: 0
Reputation: 3741
findMaxPos is a static method of your class staticMethod.
When you are not calling a static function inside the class where it is defined, you need to call it with the name of the class before:
public static void main(String[] args) {
Object[] type = new Object2[3];
yp[0] = new Object(5);
yp[1] = new Object(6);
yp[2] = new Object(8);
staticMethods.findMaxPos(type);// This should be ok.
}
Note that in java, the convention is to give classes a name which begin with an uppercase letter (Names which begin with a lowercase letters are given to instances).
Upvotes: 1