简体   繁体   中英

Call method from main and pass Array from one class to another

I am facing a roadblock with static methods.

  1. How do I call this method ?
  2. How do Pass the array to another class , so I can edit the array.

Thank you

import java.util.Scanner;

class getArray {

    public static void change(String x[]){

        Scanner keyboard = new Scanner(System.in);
        String dayName[] = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

        String[] day = new String[7];
        for(int i=0;i<7;i++){

            System.out.print(dayName[i]+ " ");
            day[i] = keyboard.nextLine();

            String str = (dayName[i]+" "+day[i]);
            x[i] = str; 

        }

        System.out.println(" ");
        for(int j=0;j<7;j++){
            System.out.println(x[j]);
        }  
    }

}

class toParse{//would parse the integer out from String[x]

}

class averageTemp{//calculate average of weather

}



public class UniSeven2 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

    }
}

As pointed out in the comment, try not using static method. That said:

import java.util.Scanner;

class getArray {

public String change(String x[]){

    Scanner keyboard = new Scanner(System.in);
    String dayName[] =  {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};

    String[] day = new String[7];
    String str;
    for(int i=0;i<7;i++){

        System.out.print(dayName[i]+ " ");
        day[i] = keyboard.nextLine();

        str = (dayName[i]+" "+day[i]);
        x[i] = str; 

    }

     return str; 
}

}

You can now access the "modified" String from whatever class you want.

Static methods belong to the Class itself, rather than the instances created by the class. Hence, when calling static method from outside it's class you would call it at the class level.

So, say we have the following..

MyClass {
     public static void myStaticMethod(int[] myIntArray) {
          //Do something
     }
     public void myNonStaticMehtod(int[] myIntArray) {
          //Do something
     }
}

When calling these methods from another class they would be called in the following ways..

Non static method Because the non static method belongs to a given instance of a class, we must first make an instance of the class and call the method from the instance.

int[] myIntArray = new int[4];
MyClass myClass = new MyClass();
myClass.myNonStaticMethod(myIntArray);

Static method The Static method belongs to the Class itself and should be called from the class level rather than from an instance of the class.

int[] myIntArray = new int[4];
MyClass.myStaticMethod(myIntArray);

On an unrelated note, the classes you are creating seem that they should be methods instead. Thinking in terms of object oriented programming, a class is the framework for an object, such as a car or person. The methods of that class should represent some action that these objects can perform. for example car.speedUp() speeds up a car, or car.getSpeed(), which gets the current speed of the car.

Hope this was helpful!

Edit: Added arrays as method parameters to help answer your second question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM