• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question #5 from Doug's Book

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code below will not compile. One line needs a single change to make it compile. What is the change required?
(Hint: This is not a simple syntax error like a missing semicolon. Don't try to play compiler. There is a more fundamental problem.)
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I believe the initialization of the instance variables & initializers are all 'lump' together with the code of the constructor to form the <init> method. So if any of those throws an exception, it must be declared in the constructor.
Is that correct?
[ August 13, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guessed the same. Good excercise for my GC lesson!
[ August 13, 2003: Message edited by: Unni Kainila ]
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c variable need to be initialized in the constructor which throws Exception
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alton has the correct answer. The rule is that if an instance variable initializer can throw an exception then every constructor must throw that exception. Good job!
Now go out an but a copy of JavaRules from amazon for less than $5!!!
[ August 14, 2003: Message edited by: Thomas Paul ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Thomas
i knew the answer Alton wrote BUT i tried to confirm it using a program.
guess what? it didnt work for me so i didnt post my answer,
here is what i wrote and got,
Program:
-------
import java.util.*;
public class ABC {
Calendar c = setCalendar();
public ABC() throws Exception {}
Calendar setCalendar() throws Exception{
Calendar rightNow = Calendar.getInstance();
if (rightNow.get(Calendar.MONTH) == Calendar.AUGUST)
throw new Exception("Don't run this in August");
return rightNow;
}
}

Result of javac:
----------------
ABC.java:5: Exception java.lang.Exception can't be thrown in initializer.
Calendar c = setCalendar();
^
1 error

then i realized why it would not work and wrote the code Prameet came up with later on. that code worked but u specified in the question that only one line needed to change to make the code compile successully and so i thought my answer is invalid
not that i want the book but just to make sure we know the correct answer.
regards
maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it may b 'coz of machine/jvm combination differences?
i 'm on,
SunOS machine 5.8 Generic_108528-10 sun4u sparc SUNW,Ultra-80
regards
maulin
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer as given is the correct answer for J2SE 1.4.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Thomas
u r right. in j2se1.4 it works!!
i decompiled the class file and got following which is same as Prameet wrote
i used 'cavaj' decompiler

regards
maulin
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:

Now go out an but a copy of JavaRules from amazon for less than $5!!!


So how do you get it for $5? It says $50 in Amazon.
[ August 14, 2003: Message edited by: Alton Hernandez ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Follow the link for "used & new".
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I bought my copy from one of these guys and it was almost perfect condition. The only thing that was less than perfect was a stamp on the back saying that it couldn't be returned to the publisher.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic