user3277606
user3277606

Reputation: 89

Class inside a class java?

Im doing a question that requires you to make a class customers which will later on be added into an array list in the method of another class. However I am getting an error on the line i marked ERROR, that says: "No enclosing instance of type Question3 is accessible. Must qualify the allocation with an enclosing instance of type Question3 (e.g. x.new A() where x is an instance of Question3)." And I have no clue why.

public class Question3 {

    static ArrayList<customers> a= new ArrayList<customers>();
    private static Scanner kbd;

    public static void main(String[] args)
    {
        String input="";
        double price=1;
        String name="";
        while(price != 0)
        {
            System.out.println("Customer Name: ");
            name= kbd.nextLine().trim();
            System.out.println("Purchase Price: ");
            price= Double.parseDouble(kbd.nextLine().trim());
            addSale(name,price);                    //ERROR
        }
    }
    public static void addSale(String name, double price)
    {
        customers c= new customers(name,price);
        a.add(c);
    }
    public class customers 
    {
        String name;
        double price;
        public customers(String name, double price)
        {
            this.name=name;
            this.price=price;
        }
    }
}

Upvotes: 0

Views: 562

Answers (6)

Eric Roberts
Eric Roberts

Reputation: 11

First off Great job so far. However, there are a couple of errors that I see in the code.

First you class should be a static class. You are trying to use static methods without a static class.

public static class Question3 {

static ArrayList<customers> a= new ArrayList<customers>();
private static Scanner kbd;

public static void main(String[] args)
{

Also, you need to create your scanner for the user to input an object.

 private static Scanner kbd = new Scanner(System.In);

Do these and your code will work perfectly!

Upvotes: 1

Mark W
Mark W

Reputation: 2803

A main method is static and thus has static context. No instance of Question3.class is required for a thread to enter that code block. Your class customers is defined inside of Question3. Because it is an inner class, it has implicit access to the fields and methods inside of the Question3 class, but it requires an instance of Question3 to be able to achieve that behavior. You need to move the code you have now in main(String args[]) into a constructor for the class Question3, and create an instance of Question3 in your main method like so :

public static void main(String args[]) {
    Question3 myQuestion3 = new Question3();
}

Alternatively as mentioned by others, you could make your customers class static. This will solve the issue by effectively making customers a top level class, but you will lose the ability to implicitly access the fields and methods of its enclosing type, which is the Question3 class.

Upvotes: 1

Praveen Kumar
Praveen Kumar

Reputation: 1624

There are two problems in your code. First , you have to initialize scanner object by providing System.in parameter to it. Second , while creating customer object you have to follow proper syntax. Here is the working code:

public class Question3 {

static ArrayList<customers> a= new ArrayList<customers>();
private static Scanner kbd=new Scanner(System.in);  // <---- Notice this 

public static void main(String[] args)
{
    String input="";
    double price=1;
    String name="";
    while(price != 0)
    {
        System.out.println("Customer Name: ");
        name= kbd.nextLine().trim();
        System.out.println("Purchase Price: ");
        price= Double.parseDouble(kbd.nextLine().trim());
        addSale(name,price);                    //ERROR
    }
    System.out.println(a);
}
public static void addSale(String name, double price)
{
    // customers c= new customers(name,price);
    Question3.customers c = new Question3().new customers(name, price); // <---Notice this 
    a.add(c);
}
public class customers 
{
    String name;
    double price;
    public customers(String name, double price)
    {
        this.name=name;
        this.price=price;
    }
} }

Upvotes: 0

Almir Campos
Almir Campos

Reputation: 3019

You also have to initialize the kbd variable as:

kbd = new Scanner( System.in );

Please review your code using this suggestion and the others above.

Upvotes: 1

Thiago Cardoso
Thiago Cardoso

Reputation: 90

Just change your inner class to a public static class:

public static class customers {

And the error disappears :)

Upvotes: 0

Karthik Kalyanasundaram
Karthik Kalyanasundaram

Reputation: 1525

You should change the declaration your class customers to solve this issue. Currently its a non-static inner class. You should change it to static inner class.

public static class customers

Non-static inner classes refers implicitly to the instance of the container class. Here you trying to create new instance of customer class in a static function, you don't have Question3 instance there.

Upvotes: 0

Related Questions