0

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 ...

4
  • 2
    you need to do staticMethods.findMaxPos Commented Dec 17, 2014 at 20:16
  • the classes are in a different file in the same folder I forgot to mention it Commented Dec 17, 2014 at 20:16
  • 4
    Well, to start your code does not even compile, for instance public Object(int a ,) is incomplete. Also it is probably a really bad idea to have a class named Object, since that is also the name of the class at the root of the Java class hierarchy. It can become really confusing when reading code. Commented Dec 17, 2014 at 20:17
  • yeah mate sorry I just typed that really fast its not my actual code that I compile just the logic of it ... I needed to type staticMethods.findMaxPos as NG. stated..Thanks guys.. gonna have to do a revision on static methods ! :) Commented Dec 17, 2014 at 20:20

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you my friend very much.. so for every static method, I need to type first the class that they are in right ?
@InFamouStarlight only if you are calling the function from another class.
You can avoid to prefix your method name by the class name using a static import: import static staticMethods.findMaxPos; or import static staticMethods.*. Then you can use it in your code as findMaxPos(type). This is available only since Java 6.
0

Well, the above solution should work but apart from that, there are a couple of other things that need to be taken into consideration.

  1. public Object(int a,) Constructor is incomplete.
  2. semicolon(;) is missing in max = name[0], pos = 0
  3. Most importantly the return type of max and Pos is undefined. POs can be int but the variable max should be of the type Object.
  4. If the return type of max is object you can't use (name[i] >max ) as it is undefined for object type. Rectify these mistakes hopefully your code will run fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.