2013-07-15 2 views
0

프로그램이 컴파일되고 오류가 발생하지 않습니다. 최신 Netbeans (Latest Java Installed)에서 프로그램을 실행할 때 출력이 표시되지 않습니다. Java 7 Third Edition 5 장의 코드에 대한 아이디어를 얻었습니다. 토론중인 주제는 new 연산자를 사용하지 않고 java.lang.class 및 객체 생성을 사용하는 것입니다.Netbeans에서 실행할 때 다음 프로그램의 출력이 표시되지 않는 이유는 무엇입니까?

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

전에 패키지 이름을 사용해보십시오;'(각각 e2''e1'과') 각'캐치에 '너를 위해 블록하고 보아라. – jlordo

+2

Reason number 2483723 빈'catch '블록은 _always_ (yes, __always__)라는 나쁜 생각입니다. 안전하게 완전히 무시 될 수있는 예외 (로그되지 않은)를 작성한 경우, 예외를 오용했습니다. –

답변

0

e.printStackTrace()`와 함께 프로그램을 다시 실행 메소드의 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. 
0

오류가있을 수 있지만 잡을 수 있지만 콘솔에 인쇄하지 않아 프로그램이 정상적으로 작동한다고 생각할 수 있습니다.

모범 사례 캐치 블록을 남기지 않고 캐치 블록을 남겨두고 오류를 인쇄하십시오. 그렇지 않으면 프로그램에서 오류를 catch하지만 경고 메시지를 표시하지 않습니다.

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가 여기에서 끝납니다.

+0

'System.out.println();을 제외하고는 모든 정보를 얻기 위해'e.printStackTrace();'를 사용하십시오. – jlordo

+0

나는 e1.printStackTrace()를 사용했다; 및 e2.printStackTrace(); 프로그램이 실행되고 Netbeans IDE에서 BUILD SUCCESSFUL (총 시간 : 1 초) 메시지가 표시되지만 출력이 표시되지 않습니다. –

+0

@AjmalKhan 나는 Maven을 사용하여 프로젝트를 구축하고 있지만, 전혀 실행하지 않고 있다고 생각한다. – orique

관련 문제