Pergunta

I have four classes in my program in which One contains main() but there is a error in first three lines in fruit, ch and demand variable. I want to use these variables in each class.

    import java.util.Scanner;


public static int fruit = 0;
public static int ch;
public static boolean demand = false;

    class stock{
        synchronized int getfruit(){
            while(demand){
                try{
                    wait();
                }catch(InterruptedException e){
                    System.out.println("Wait fruits uploading");
                }

                System.out.println("cutomer got : " + ch);
                demand = true;
                notify();
            }
            return ch;
        }

        synchronized void putfruits(int ch){
            while(!demand){
                try{
                    wait();
                }catch(InterruptedException e){
                    System.out.println("uploaded already");
                }
                System.out.println("Uploading your demand : "+ ch + "  fruits");
                demand = false;
                notify();
            }
        }
    }
    class vendor implements Runnable{

        stock obj;
        public vendor(stock obj) {
            // TODO Auto-generated constructor stub
        this.obj = obj;
        new Thread(this, "vendor").start();
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            obj.putfruits(ch);
        }

    }

    class customer implements Runnable {

        stock obj;
        public customer(stock obj) {
            // TODO Auto-generated constructor stub
        this.obj = obj;
        new Thread(this, "cutomer").start();
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            obj.getfruit();
        }

    }
    public class Fruitmarket {
    public static void main (String args[]){
    stock obj2 = new stock();
    System.out.println("Initially market no capacity");
    System.out.println("Enter how much quantity you want ?");
    Scanner in = new Scanner ( System.in);
    ch = in.nextInt();

}
}

How can I do this ? I'm beginner in java ?

Foi útil?

Solução

This code will not compile. You need to put these variables inside a class like so:

import java.util.Scanner;


class stock {

    public static int fruit = 0;
    public static int ch;
    public static boolean demand = false;

    ....

If you want to access these variables outside, you can do this by referring to it like this: stock.fruit

Outras dicas

If you want to use constants, you can declare an Interface, which you will then use to store constants as public static final <type> <name>

EDIT : You should not implement the interface, instead simply call it : InterfaceName.CONSTANT

It is also good practice to name your constants in all capital letter.

EDIT 2 : It seems that since java 5 (I Guess I'm really outdated...) te use of static imports is considered better practice

Outside the Class definition you can't declare variables. Like the other guys said, you need to declare them inside the class.

If they are public and declared inside class Stock, you can access them from any other class. If they are friendly (no keyword, just static int fruit = 0;) you can access them from any class in the package. How to access them will depend on whether they're static or not. static fields can be accessed by referencing the class itself (e.g. stock.fruit), while for non-static fields you'll need to reference an object that is an instance of the class holding the field.

Now, with that said, I would very much recommend that you declare your variables where they should logically be placed, according to the context of the program. I'm not entirely sure what demand and ch are intended to mean, but just know that you can put them in any class, including Fruitmarket, so long as they have the correct access level modifiers (public, friendly, etc) and you attempt to access them in the right way (through the declaring class if they are static or through an instance of the declaring class if they are non-static)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top