简体   繁体   中英

Getting a public static void class to read an array from another class

Here is said class

public static void showCodes(){
            System.out.print("  Select from");
            System.out.print("  =>  ");
            for (String show : productDB.inventory)
            { System.out.print(show.toUpperCase() + " ");}
            System.out.print("  <=");
     }

Here is the other class (that has the array)

public class ProductDB
{
public String[] inventory = {"java", "jsps", "mcb2", "txtp", "calc", "topg", "bigl", "sgtp"};
**insert rest of class code**
}

this is my main

public static void main(String args[])
{
System.out.println("  Product Selection\n");
showCodes();
  ...

I understand that I need to define my variable, but I'm fairly new to Java. In the first block code, on line 4, I get a "cannot find symbol" error.

Change for loop in the method showCodes to:

for (String show : ProductDB.inventory)

and add static to the array definiation in the class, as following:

public static String[] inventory = {"java", "jsps", "mcb2", "txtp", "calc", "topg", "bigl", "sgtp"};

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