문제

The program compiles and gives no error. When I run the program in the latest Netbeans (Latest Java Installed), I don't see the output. I have taken the idea for the code from book Java 7 Third Edition chapter 5. The topic under discussion is using java.lang.class and creation of an object without using the new operator.

package java7thirdeditionpart1;

public class creatObjectWithoutNewOperator {

    public static void main(String[] args) {

        Class myClass2 = null;
        try {
            myClass2 = Class.forName("Book");
        } catch (ClassNotFoundException e) {

        }

        if (myClass2 != null) {
            try {
                //Creating an instance of the Book class
                Book book1 = (Book) myClass2.newInstance();                
                book1.setAuthor("Khan");
                System.out.println(book1.getAuthor());
                book1.setTitle("Second Book");
                book1.setIsbn("kh_s_b");                
                book1.printBookDetails();
            } catch (IllegalAccessException e1) {
                  e1.printStackTrace();

            } catch (InstantiationException e2) {
                  e2.printStackTrace();

            }
        }

    }//main method ends here.
}//class creatObjectWithoutNewOperator ends here.

package java7thirdeditionpart1;

public class Book {
    String isbn;
    String title;
    String author;

    public Book()
    {
        this.setIsbn("");
        this.setTitle("");
        this.setAuthor("");
    }//Constructor ends here.

    public Book(String isbn, String title, String author) {
        this.setIsbn(isbn);
        this.setTitle(title);
        this.setAuthor(author);
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void printBookDetails(){
        System.out.println("*********************");
        System.out.println("ISBN: " + this.getIsbn());
        System.out.println("Title: " + this.getTitle());
        System.out.println("Author: " + this.getAuthor());
        System.out.println("*********************");
    }//method printBookDetails ends here.

}//Class Book ends here.
도움이 되었습니까?

해결책

Try using the package name before the class in the method forName()

package java7thirdeditionpart1;

public class creatObjectWithoutNewOperator {

    public static void main(String[] args) {

        Class myClass2 = null;
        try {
            myClass2 = Class.forName("java7thirdeditionpart1.Book");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        if (myClass2 != null) {
            try {
                //Creating an instance of the Book class
                /*Since newInstance returns a
java.lang.Object object, you need to downcast it to its
original type.*/
                Book book1 = (Book) myClass2.newInstance();                
                book1.setAuthor("Khan");                
                book1.setTitle("Second Book");
                book1.setIsbn("kh_s_b");                
                book1.printBookDetails();

                book1 = (Book) myClass2.newInstance();
                book1.setAuthor("Ajmal");
                book1.setTitle("First Book");
                book1.setIsbn("aj_f_b");
                book1.printBookDetails();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (InstantiationException e2) {
                e2.printStackTrace();
            }
        }

    }//main method ends here.
}//class creatObjectWithoutNewOperator ends here.

다른 팁

You might have an error and you catch it, but you do not print it on the console, thus thinking that the program works ok.

As a best practice never leave a catch block without some form of printing the error. Otherwise the program will catch errors, but it will not display a warning message.

public static void main(String[] args) {

    Class myClass2 = null;
    try {
        myClass2 = Class.forName("Book");
    } catch (ClassNotFoundException e) {
        System.out.println("Error: " + e);
    }

    if (myClass2 != null) {
        try {
            //Creating an instance of the Book class
            Book book1 = (Book) myClass2.newInstance();                
            book1.setAuthor("Khan");
            System.out.println(book1.getAuthor());
            book1.setTitle("Second Book");
            book1.setIsbn("kh_s_b");                
            book1.printBookDetails();
        } catch (IllegalAccessException e1) {
            System.out.println("Error1 " + e1);               
        } catch (InstantiationException e2) {
            System.out.println("Error2 " + e2);
        }
    }

}//main method ends here.

}//class creatObjectWithoutNewOperator ends here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top