Prev: Hashcode and Equal
Next: Speaking of thread safety?
From: RedGrittyBrick on 10 Mar 2010 04:05 On 10/03/2010 08:50, Javas wrote: > On Mar 10, 9:31 am, Lew<no...(a)lewscanon.com> wrote: >> Please reply to the list. Eric Raymond collected some notes on getting the best out of Usenet. I found them useful, maybe you will too. References below. >> Javas wrote: >>> Guruji! Don't try to act smart. Just try to share your views and >>> convey your requests in a cordial mannar. Hope you understand your >>> mistakes. I'll correct me from my next post onwards. Anyways, Thanks >>> for your answer. >> >> #1: If you're so smart, why are you asking us for help? > > I never advertised to you that I am smarter than > you. I guess you are trying to get personal. http://www.catb.org/~esr/faqs/smart-questions.html#not_losing > >> #2: What wasn't cordial about my manner? > > You could have just answered the questions and > stopped there, instead of worrying about whether's it my homework or > assignment work. Or atleast you could have suggested me to name the > class properly. But you chose the wrong path. http://www.catb.org/~esr/faqs/smart-questions.html#homework > >> #3: What mistakes? Trying to help you, apparently. > > You had some other intentions too other than > helping one alone. > > Above all, I just wanted to convey it to you. > That's the reason why I sent it to you alone. http://www.catb.org/~esr/faqs/smart-questions.html#noprivate -- RGB
From: Joshua Cranmer on 10 Mar 2010 07:37 On 03/10/2010 12:53 AM, Javas wrote: > Why does the above code results in a compilation error? since it is > always invoking class B's process() method which doesnt throw any > exception. How can the compiler know that you're only invoking B's method? -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
From: Lew on 10 Mar 2010 07:56 Javas wrote: >>> Guruji! Don't try to act smart. Just try to share your views and >>> convey your requests in a cordial mannar. Hope you understand your >>> mistakes. I'll correct me from my next post onwards. Anyways, Thanks >>> for your answer. Lew wrote: >> #1: If you're so smart, why are you asking us for help? Javas wrote: > I never advertised to you that I am smarter than you. Nor did I. > I guess you are trying to get personal. And you weren't? Lew wrote: >> #2: What wasn't cordial about my manner? Javas wrote: > You could have just answered the questions and > stopped there, instead of worrying about whether's it my homework or > assignment work. Or atleast you could have suggested me to name the > class properly. But you chose the wrong path. You did name the class properly, afaict, and what is wrong about my "path"? Who said I was "worried"? This is a discussion group, not your little unpaid help-slave labor pool; I was discussing. Helpfully, too, though you missed that. Lew: >> #3: What mistakes? Trying to help you, apparently. Javas wrote: > You had some other intentions too other than > helping one alone. Oh? You misread my mind there, Kreskin. Everything I said in my first response was to help you. > Above all, I just wanted to convey it to you. > That's the reason why I sent it to you alone. Usenet is a public forum for open discussion. -- Lew
From: Patricia Shanahan on 10 Mar 2010 09:45 Joshua Cranmer wrote: > On 03/10/2010 12:53 AM, Javas wrote: >> Why does the above code results in a compilation error? since it is >> always invoking class B's process() method which doesnt throw any >> exception. > > How can the compiler know that you're only invoking B's method? > In this case the compiler *could* know through data flow analysis. The method invocation is dominated by the assignment of the result of a B instance creation to the invocation's target reference. In order to avoid situations in which a program is considered valid by one compiler but rejected by another, the compile time error rules are written up in the JLS. Allowing the example program would require a set of rules at least as complicated as the definite assignment rules at http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html. The benefit would be far smaller, and would destroy the advantage of declaring the variable with a supertype. If the programmer intends the reference to be required to refer to a B, and treated by the compiler as always referring to a B, it should be declared as type B. The usual motive for using a supertype such as A is to ensure that the code does not take advantage of the variable always referring to a B, and would continue to work if "new B()" were replaced by an expression referring to an A or to any subclass of A. Data flow analysis acceptance of the example would destroy that benefit. Patricia
From: Jeff KE7NVY on 10 Mar 2010 11:49 On Mar 9, 9:53 pm, Javas <deepan...(a)gmail.com> wrote: > public class Qn24 { > > static class A > { > void process() throws Exception { throw new > Exception(); } > } > > static class B extends A > { > void process() { System.out.println("B "); } > } > > public static void main(String[] args) > { > A a = new B(); > a.process(); > } > > } > > Why does the above code results in a compilation error? since it is > always invoking class B's process() method which doesnt throw any > exception. Exception is a checked exception. That means that whenever you declare a method that (potentially) throws it, you must do one of two things in the code that calls that method. Either handle the exception by putting the call inside a try/catch block: public static void main(String[] args) { A a = new B(); try { a.process(); } catch (Exception e) { System.out.println("main: Exception caught"); } } Or, declare the calling method as also throwing Exception: public static void main(String[] args) throws Exception { A a = new B(); a.process(); }
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Hashcode and Equal Next: Speaking of thread safety? |