Confusion about how to access methods?

J

Joan

I am beginner in Java programming. I have a question about

I know that you can't reference a method with a variable that is not
initialized with a newly constructed object or an existing object. So
the following would have a compile-time error:

(reference the book Core Java 2 Volumn I - Fundamentals by Horstmann &
Cornell page 121-122)
Date deadline;
s = deadline.toString();

Instead, the following is right:

Date deadline = new Date();
s = deadline.toString();


My question is: according to this rule, how can code like the
following also works?

catch(Exception e) {
e.printStackTrace();
}

So the variable e is not yet initialized. How can it access the method
printStackTrace()?

Please kindly advise. Thank you.

Joan
 
A

Andrew Thompson

| I am beginner in Java programming.

perhaps you will getter better help at..
for the moment.

| catch(Exception e) {
| e.printStackTrace();
| }
|
| So the variable e is not yet initialized.

It most certainly is. The Exception object
'e' is instantiated by the class or method that
'throw's the exception object.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top