1

I have already make a class "CdFilm" in different file with main class. But I can't instantiate the new object with my own class. I instantiate it like this :

FileRentalVCD.CdFilm film = new FileRentalVCD.CdFilm(1);

This is the class "CdFilm" file :

public class FileRentalVCD {
    private String judul, publisher;
    private char kategori;
    private int stok;


    public class CdFilm {
        //inheritance from FileRentalVCD
        private String judul, publisher;
        private char kategori;
        private int stok;
        //atribut class CDFilm
        private String pemain, sutrdara;

        //constructor
        public CdFilm (int s) {
            this.stok = s; 
        }
        //methods encapsulation

        public String getJudul() {
            return judul;
        }

        public void setJudul(String judul) {
            this.judul = judul;
        }
    }
}

And this is the main class, where i instantiate my object :

public class RentalVCD {

    public void EntriCdFilm (FileRentalVCD.CdFilm input) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Masukan Judul : ");
        String judul = scan.next();
        input.setJudul(judul);
    }

    public static void main(String[] args) {
        System.out.println("Rental VCD Alif");
        System.out.println("1. Entri data CdFilm");
        System.out.println("2. Entri data CdMusik");
        System.out.println("3. Tampilkan data CdFilm");
        System.out.println("4. Tampilkan data CdMusik");
        Scanner scan = new Scanner(System.in);
        int pilihan = scan.nextInt();
        FileRentalVCD.CdFilm film = new FileRentalVCD.CdFilm(1);
    }
}
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
alramdein
  • 810
  • 2
  • 12
  • 26
  • Why are you wrapping one class inside of another unnecessarily? This only adds complexity to what should be a simple situation. – Hovercraft Full Of Eels Oct 07 '18 at 14:13
  • Also, if you search on the key words of your problem, [java problem instantiating inner class site:stackoverflow.com](https://www.google.com/search?q=java+problem+instantiating+inner+class+site:stackoverflow.com), you would find many similar and even duplicate questions. – Hovercraft Full Of Eels Oct 07 '18 at 14:17
  • I don't know, i just try to figure out about the question on my homework. And that is what i think which is implemented in java. If you had any solution it would be helpful – alramdein Oct 07 '18 at 15:24
  • What do you mean by "and if you had any solution..."? I gave you a link to **many** solutions, but more than that, to a decent search strategy so that in the future you can find solutions to similar questions quickly. – Hovercraft Full Of Eels Oct 07 '18 at 17:50

1 Answers1

2

You can, just not with that syntax. It's somewhat counter-intuitive, but you can use the following to create an instance of your inner class:

FileRentalVCD.CdFilm film = new FileRentalVCD(/* add any args here */).new CdFilm(1);

This is needed because you need an instance of your outer class to create an instance of the inner class.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116