• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

when the block code is execute?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AllFinals
{
{
final Vector v;
v=new Vector();
}
public AllFinals()
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
}
An attempt to compile and call the someMethod of the above class at runtime will cause

A) A compilation error : v is not initialized in all constructors
B) A compilation error : v is not an instance variable
C) "true" is printed on the console
D) "false" is printed on the console
The given ans is;B.which had been proved right.
But i think the block
{
final Vector v;
v=new Vector();
}
is execute once the class AllFinals is loaded,which is seems like the memebre field,as a result,the compile give me error as the ANS B said,but when the block code is execute?
 
Sheriff
Posts: 16910
283
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not with the code that's in it, it won't. That's the point of the question. However, if the block (an initialization block) contained legal code, it would be executed each time a new class instance is created.
See the JLS section on Instance Initializers (�8.6) for details.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"v" is not an instance variable. It's a local variable, defined inside a code block and accessible only from within that code block.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think the source of confusion is the difference between a block of static initialisation code & a normal block of initialisation code.
For example
class Test {

 
miceal oconnor
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops!! Sorry about that ..anyway I'll start again.
I think the source of confusion is the difference between a block of static initialisation code & a normal block of initialisation code.
For example
class Test {
// Normal Initialisation block
{
// code
}
}
In the case shown the initialisation block is executed every time the class is instantsiated. However if you insert a 'static' keyword in from of the opening brace the code will be executed when the class in loaded & not when it is instansiated.
I think I have that right ... anybody got any objections?

 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:
<pre>public class AllFinals
{
{
final Vector v;
v=new Vector();
}
public AllFinals()
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
}
</pre>
.........................
But i think the block
{
final Vector v;
v=new Vector();
}
is execute once the class AllFinals is loaded,which is seems like the memebre field,as a result,the compile give me error as the ANS B said,but when the block code is execute?



Hi
this is not static block so it will be executed when an instance will be created.
but point is that " Vector v " is local variable here not a instance variable , generally we assume that after class name variable are instance variable . This is good question..... which says READ Que. CAREFULLY and MARK the blocks.

------------------
Regards
Ravish
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic