• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InstantiationException

 
deepti bellubbi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think deepti ,you are correct
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it. Thank you all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic