• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Static final blank variable

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried doing something of declaring a static blind final variable and tried to initialize it in the static block. Problem is my initilialization theows a checked exception. Now im trying to catch using try catch block but it gives problem.
Is there any way this can be done in java.
class A {
public static final String out;
// they cant initialize out here because calling the constructor //of printstream will cause an exception
// now they can put it in a static block but unfortunately this //code gives an unitialized error thatz valid because what if an //exception occurs

/* static {
try{
out = "string";
}catch(Exception e){}
} */
//now i tried this we cant use this also i dont know why
static {
try{
out = "string";
}catch(Exception e){out = "string";}
}
}
even this gives error but why when im initializing the final variable in all cases ie, if an exception occurs and also in the normal flow.
Cherry
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cherry Mathew:
I think the follwing Code will work Just try this out.

- Hope this Helps.
Siva
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cherry,
u can only intialize final variable while u r declaring
it or in the constructor of the class.
I hope this'll help u out of your problem
Ashish
 
Sivalingam Sivasuthan
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ASHISH KUMAR,
You can assign a value for a Final variable within the initializers(Or free floating code block).But if it is a static final variable then you have to use static code block.

Originally posted by ASHISH KUMAR:
Hi Cherry,
u can only intialize final variable while u r declaring
it or in the constructor of the class.
I hope this'll help u out of your problem
Ashish


-Siva
[This message has been edited by Sivalingam Sivasuthan (edited February 20, 2001).]
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Cherry:
It seems that the compiler will not recognize the initialization in a try or catch block because it doesn't know what will happen at runtime(might not complete normally). But in the case of finally, the initialization will always get executed(at least the compiler thinks so). Java has a lot of subtle fuss in terms of compiling. My posting is another example: http://www.javaranch.com/ubb/Forum24/HTML/008067.html
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic