getting error when compiling.
"java:60: non-static variable inventory cannot be referenced from a static context for (String show : ProductDB.inventory)"

the showCodes class has to public static void. Here is code

import java.util.*;
import java.text.*;

public class ProductApp
{
    public static void main(String args[])
    {
		
        // display a weclome message
        System.out.println("  Welcome to the Product Selector\n");

        // perform 1 or more selections
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while (choice.equalsIgnoreCase("y"))
        {
            System.out.print("  Enter product code or X to Exit: ");
            String productCode = sc.next();  // read the product code
            sc.nextLine();  // discard any other data entered on the line

            // see if the user wants to continue
            if ("x".equals(productCode)||("X".equals(productCode))) {
				System.out.printf("\n");
				System.out.printf("  Program terminated by the user ...\n");
				System.out.printf("\n");
				break;
			}
			else {

            // get the Product object
            Product p = ProductDB.getProduct(productCode);

            // display the output
            System.out.println();
            if (p != null)
                System.out.println(p.toString());
            else
                System.out.println("  No product matches this product code.\n");

            System.out.println("  Product count: " + Product.getCount() + "\n");

            // see if the user wants to continue
            showCodes();
            System.out.println();
            //System.out.print("  Continue? (y/n): ");
            //choice = sc.nextLine();
            System.out.println();
		}	// end if
        }	// end while
    }	// end main

    public static void showCodes()
    {
		System.out.println("  You have these product codes to choose from:\t");
		for (String show : ProductDB.inventory)
		{
			System.out.print(show + " ");
		}	// end for loop
	}	// end showCodes method
}	// end class ProductApp

Recommended Answers

All 3 Replies

where's ProductDB.inventory declared? you can't just start referencing variables in a function when they were declared in main. You might want to pass it as an argument or declare it somewhere else. :D

where's ProductDB.inventory declared? you can't just start referencing variables in a function when they were declared in main. You might want to pass it as an argument or declare it somewhere else. :D

ProductDB is a completely different class than ProductApp, and inventory is a variable in ProductDB that is not declared as static. But he is treating the 'inventory' variable as if it was static, which is why the compiler is complaining. The solution is to either declare inventory as a static variable, or to create a ProductDB object (which he already did, in main, as you said), pass it into the method, and use it to access the inventory variable.

thanks. i declared inventory as a static variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.