• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please answer my query?

 
payal sharma
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to understand this?

here why I am getting compile time error as AllFinals.java:7: Identifier expected.
v=new Vector();
Although I am changing the refrence of the final object not the object itself.
I can change its reference in contructor if I put v=new Vector() in constructor.
but here

AllFinals.java:5: Blank final variable 'v' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.
final Vector v;
but If assigned value in initializer then

I am geting error
AllFinals.java:15: Undefined variable or class name: v
System.out.println(v.isEmpty());
^
Now
static {
final Vector v;
v=new Vector();
}
making initializer static giving the same problem why?
^
Q2 Why top level class can not be static?
Q3

here object a1 is of tyep AnInterface but reference of AnInterfaceImpl.
Call method on this object is giving compile time error methodOne do not catch or throw Exception.
But why

this compiles and with out and error or exception.
Q4
why

this giving not equal
 
Bill Tripper
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1: The statement "v = new Vector()" is floating out there where your variable declarations belong. You can put it into an initializer block, e.g., "{v = new Vector();}" and it will work.
Instead of:
{
final Vector v;
v=new Vector();
}
use:
final Vector v;
{
v = new Vector();
}
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or why not just code:
final Vector v = new Vector();
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ya this will also work or rather should.

Originally posted by Thomas Paul:
or why not just code:
final Vector v = new Vector();

 
wei luo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think "final Vector v;
public AllFinals()
{ v = new Vector();
}
will also work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic