wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes InstantiationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "InstantiationException" Watch "InstantiationException" New topic
Author

InstantiationException

deepti bellubbi
Greenhorn

Joined: May 06, 2008
Posts: 27
Hi,
One of the questions of J2EECertification--
Assume the following class and interface, defined in different source files and same package, will the following JSP tag work? <jsp:useBean id="mobile" scope="session" class="Device" type="Phone" />

1. public interface Device {
2. }

1. public class Phone implements Device{
2. }

1. Yes. The phone class implements correctly the Device interface.
2. No. The class attribute Device is not a superclass of the class Phone.
3. No. The attribute class and type cannot be used together.
4. Yes. The useBean Tag is implemented correctly.
5. No. An InstantiationException is thrown at compile-time.

Answer 2 is correct.

My doubt is what change in the above code could cause an InstantiationException?

Thanks
Deepti
Ravi Singh
Greenhorn

Joined: Jun 02, 2008
Posts: 26
<jsp:useBean id="mobile" scope="session" class="Device" type="Phone" />

In the above statement if there is no object value set to the mobile attribute .
Then it will cause instantiation Exception as in then the container will try to create an instance of Device wich is an interface.

code that follows is


Phone mobile = (Device) pageContext.getAttribute("mobile",PageContext.PAGE_SCOPE);

if(mobile=null)

{
Phone mobile = new Device() //This will cause error if there is no mobile attribute already set bcos of if condition

}
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

i think deepti ,you are correct
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129
I tested the code just like Deepti said but I did not get Instantiation Exception.It is just a JspException.

So I think question remains unanswered as Deepti asked for the change in the code that would cause the IE.

Thanks,
Amol


"There are no mistakes, only lessons"
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
The above code wouldn't throw any exception from knowledge, it will rather show a compilation error like : "Can't instantiate the Device", as we can't create a object of interface type e,g. new Device().

And here the changes made to get java.lang.InstantiationException:
1:Remove class attribute from <jsp:useBean>
<jsp:useBean id="mobile" scope="request" type="bean.Device" />

2: And set attribute of type Device somewhere in your jsp or servlet like :
in a jsp which will forward request to the another jsp where jsp:useBean has been used.
<%
Phone mobile = new Phone();
session.setAttribute("mobile",mobile) ;
%>

Now if run this we will definetly get and InstantiationException as we set the attribute in "session" and trying to get it in "request" scope and we dont have a class attrribute.
This all changes successfully get back youaInstantiationException ,which I think required by you.
deepti bellubbi
Greenhorn

Joined: May 06, 2008
Posts: 27
I get it. Thank you all.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: InstantiationException