0

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.
  • 1
    rerun the program with `e.printStackTrace();` (`e1` and `e2` respectively) in each `catch` block and see for yourself. – jlordo Jul 15 '13 at 12:49
  • 2
    Reason number 2483723 that an empty `catch` block is _always_ (yes, __always__) a bad idea. If you have created an exception that can safely be completely ignored (not even logged), you have misused exceptions. – Colin M Jul 15 '13 at 12:51

2 Answers2

0

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.

Mythul
  • 1,807
  • 7
  • 34
  • 53
  • don't just `System.out.println();` the exception, use `e.printStackTrace();` to get all the information. – jlordo Jul 15 '13 at 13:05
  • I have used e1.printStackTrace(); and e2.printStackTrace(); but the program runs and gives a message BUILD SUCCESSFUL (total time: 1 second) in Netbeans IDE but I don't see the output. – Ajmal Khan Jul 15 '13 at 13:30
  • @AjmalKhan I think you are just building the project with Maven but you aren't running it at all. – orique Jul 15 '13 at 13:31
  • I use the right menu command "Run File" as this file has its own main method so the output should be visible. I did the same with other small programs each in a separate file with its own main method and they worked. so I don't run the project but I run the current file only. – Ajmal Khan Jul 15 '13 at 13:39
0

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.
Dilshad Rana
  • 148
  • 9