• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Initialization

 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
v is final, cause of that, you must initialize it before contructor ends.
 
Mahendra Shelke
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final variables can assign a value once.the compiler will not initilaze any default value

Narendra
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for the explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic