0

I've a mother class livre.

Bd & album are two extended classes from Livre

I've a problem on the main(), I can't initiate & declare an object (myAlb) from the class album: here is what i did:

album[] myAlb;

myAlb= new album[nbr_of_albums];

myAlb[i] = album(1,5,"author","title"); // for an album i I call the constructor of album= error

here is the error: No enclosing instance of type livre is accessible. Must qualify the allocation with an enclosing instance of type livre (e.g. x.new A() where x is an instance of livre).

here is my full code source:

import java.util.*;

public class livre {

public abstract class book {
    String titre;
    String auteur;
    float prix;
    int nbr_pages;
    book(String titre,String auteur, float prix,int nbr_pages){
        this.titre = titre;
        this.auteur = auteur;
        this.prix = prix;
        this.nbr_pages = nbr_pages;
    }
    abstract void affichage();
}
public class bd extends book {
    String couleur;
    bd(String titre,String auteur, float prix,int nbr_pages,String couleur){
        super(titre,auteur,prix,nbr_pages);
        this.couleur = couleur;
    }
    void affichage(){
        System.out.println("\n\nbook:"+titre);
        System.out.println("+ auteur"+auteur);
        System.out.println("+ prix"+prix);
        System.out.println("+ nbr_pages"+nbr_pages);
        System.out.println("+ "+couleur);
    }

}
public final class album extends book {
    String [] couleur;
    void changerCouleur(){
        int nbr = 0;
        System.out.print("Plz set the nbr of the page that you want to color: ");
        Scanner sc = new Scanner(System.in);
        while (!(nbr<= nbr_pages && nbr > 0 )){ nbr = sc.nextInt();}
        System.out.print("Plz set what color u wanna colorate this page: ");
        couleur[nbr] = sc.nextLine();
        sc.close();
    }
    void affichage(){
        System.out.println("\t\t book:"+titre);
        System.out.println("+ auteur"+auteur);
        System.out.println("+ prix"+prix);
        System.out.println("+ nbr_pages"+nbr_pages);
        System.out.println("+ couleurs des pages: ");
        for(int i=0;i<nbr_pages;i++) System.out.println("   =>Page["+i+"]= "+couleur[i]);
    }
    album(String titre,String auteur, float prix,int nbr_pages){
        super(titre,auteur,prix,nbr_pages);
        couleur = new String[nbr_pages];
    }
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    album[] myAlb; bd[] myBd;

    Scanner sc = new Scanner(System.in);
    System.out.print("Set the nbr of Albums that you want to make: ");
    int nbrAlbum = sc.nextInt();
    myAlb= new album[nbrAlbum];
    System.out.print("Set the nbr of BD that you want to make: ");
    int nbrBd = sc.nextInt();
    myBd= new bd[nbrBd];
    for(int i=0;i<nbrAlbum;i++){
        System.out.print("\tAlbum nbr "+i+": ");
        System.out.print("=>titre = ");
        String titre = sc.nextLine();
        System.out.print("=>auteur = ");
        String auteur = sc.nextLine();
        System.out.print("=>prix = ");
        float prix = sc.nextFloat();
        System.out.print("=>nbr de Pages = ");
        int nbr_pages = sc.nextInt();
        myAlb[i] = new album(titre,auteur,prix,nbr_pages);

    }

}

}

Yassine Bakkar
  • 107
  • 1
  • 13
  • Please format your code properly. Also, please post the exact compiler error. – Turing85 Nov 10 '15 at 22:12
  • my code is well formatted I suppose. and yes i did post the compiler error: No enclosing instance of type livre is accessible. Must qualify the allocation with an enclosing instance of type livre (e.g. x.new A() where x is an instance of livre). – Yassine Bakkar Nov 10 '15 at 22:17
  • It pretty much looks like each of your inner classes (book, bd, album) should be static, and that alone would fix your code. – Louis Wasserman Nov 10 '15 at 22:17
  • Your code is not well-formated (there is a indentation missing on the level of the class) and you did not post the exact compile error. A compiler error always tells the exact line of the error. – Turing85 Nov 10 '15 at 22:19
  • @Turing85 sorry I guess that i wasn't that clear it's on: "myAlb[i] = new album(titre,auteur,prix,nbr_pages);" i mentioned it on the top – Yassine Bakkar Nov 10 '15 at 22:26
  • @LouisWasserman my bad, works like a charm, can you please give me more details on why we should use static ? i read here that we use it if a class method don't change directly its attributes i guess that this is not true – Yassine Bakkar Nov 10 '15 at 22:26
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:22

2 Answers2

0

Replace public abstract class book with public static abstract class book

And replace public final class album extends book with public final static class album extends book

That should work. Adding static modifier allows you to instantiate an inner class without having an instance of the enclosing class. You have the book class as an inner class, that's why you need the static modifier.

If you also want to instantiate bd class, you should also put static for it.

Joel Min
  • 3,387
  • 3
  • 19
  • 38
0

Your Code can be rewritten as below:

public class livre { public static void main(String[] args) {

    abstract class book {
        String titre;
        String auteur;
        float prix;
        int nbr_pages;
        book(String titre,String auteur, float prix,int nbr_pages){
            this.titre = titre;
            this.auteur = auteur;
            this.prix = prix;
            this.nbr_pages = nbr_pages;
        }
        abstract void affichage();
    }

    class bd extends book {
        String couleur;
        bd(String titre,String auteur, float prix,int nbr_pages,String couleur){
            super(titre,auteur,prix,nbr_pages);
            this.couleur = couleur;
        }
        @Override
        void affichage(){
            System.out.println("\n\nbook:"+titre);
            System.out.println("+ auteur"+auteur);
            System.out.println("+ prix"+prix);
            System.out.println("+ nbr_pages"+nbr_pages);
            System.out.println("+ "+couleur);
        }

    }

    final class album extends book {
        String [] couleur;
        void changerCouleur(){
            int nbr = 0;
            System.out.print("Plz set the nbr of the page that you want to color: ");
            Scanner sc = new Scanner(System.in);
            while (!(nbr<= nbr_pages && nbr > 0 )){ nbr = sc.nextInt();}
            System.out.print("Plz set what color u wanna colorate this page: ");
            couleur[nbr] = sc.nextLine();
            sc.close();
        }
        @Override
        void affichage(){
            System.out.println("\t\t book:"+titre);
            System.out.println("+ auteur"+auteur);
            System.out.println("+ prix"+prix);
            System.out.println("+ nbr_pages"+nbr_pages);
            System.out.println("+ couleurs des pages: ");
            for(int i=0;i<nbr_pages;i++) System.out.println("   =>Page["+i+"]= "+couleur[i]);
        }
        album(String titre,String auteur, float prix,int nbr_pages){
            super(titre,auteur,prix,nbr_pages);
            couleur = new String[nbr_pages];
        }
    }


    album[] myAlb; bd[] myBd;

    Scanner sc = new Scanner(System.in);
    System.out.print("Set the nbr of Albums that you want to make: ");
    int nbrAlbum = sc.nextInt();
    myAlb= new album[nbrAlbum];
    System.out.print("Set the nbr of BD that you want to make: ");
    int nbrBd = sc.nextInt();
    myBd= new bd[nbrBd];
    for(int i=0;i<nbrAlbum;i++){
        System.out.print("\tAlbum nbr "+i+": ");
        System.out.print("=>titre = ");
        String titre = sc.nextLine();
        System.out.print("=>auteur = ");
        String auteur = sc.nextLine();
        System.out.print("=>prix = ");
        float prix = sc.nextFloat();
        System.out.print("=>nbr de Pages = ");
        int nbr_pages = sc.nextInt();
        myAlb[i] = new album(titre,auteur,prix,nbr_pages);

    }
}

}

For more info on nested class follow the below link: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Rajdip
  • 21
  • 5