This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object Initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object Initialization" Watch "Object Initialization" New topic
Author

Object Initialization

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
import java.util.*;

public class AllFinals
{
final Vector v;
public AllFinals()
{
}
}

The above code will


1.Not compile. Vector v is not initialized.
2.Will compile and throw a RuntimeException
3.Will compile and not throw any Exception during runtime. V is initalized to null.

Answer is 1.
My question is v should get initialized to null (default value for an object) and if that is the case answer should be 3.
can anybody explain?
Antonio Trapero
Greenhorn

Joined: Mar 24, 2007
Posts: 11
v is final, cause of that, you must initialize it before contructor ends.
Mahendra Shelke
Greenhorn

Joined: May 08, 2006
Posts: 20
You are correct the answer is 1 , that is because v is final without any initialization parameters i.e it is a blank final and must be initialized in the constructor to null or any other valid value you wish to.
karnatam narendraprasad
Ranch Hand

Joined: Mar 01, 2007
Posts: 30
final variables can assign a value once.the compiler will not initilaze any default value

Narendra
Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
Thanks to all for the explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Initialization
 
Similar Threads
Generics Question
please answer my query?
when the block code is execute?
Block - Initialisation
java