Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Akash Kumar
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I am not able to find error in the following code. Is it due to the static method getString() doesn't have access to buffer OR C1 constructor is public, while the class has no modifier OR something else.

Thanks in Advance
Jess added UBB [CODE] tags to preserve whitespace]
[ July 19, 2002: Message edited by: Jessica Sant ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I tested this code and I got this:
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try compiling it? Did you get an error message? -- I got this one:

you're trying to reference the object 'buffer' before you've defined it. Try putting the def'n / instantiation before you use it -- and see what happens.
Also, check out 8.7 Static Initializers in the JLS.
 
Veena Pointi
Ranch Hand
Posts: 442
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run a java programs static blocks are executed first.They are executed in the order they are declared.In your code in first static block you are appending buffer before it is instantiated as indicated by compiler error.
Try to put private static StringBuffer buffer=new StringBuffer statement before the static block where you are appending it.
Veena
 
Chung Huang
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that a static block of code? 'Coz if it were, then wouldn't it be a initializer for the class as whole? Then what happen is that as soon as the class is loaded, that block of static codes get to be executed when buffer wasn't even declared yet. So either put the declaration inside of the static block of code (before the statement where you use it), or you could put the statement that uses it inside a method or constructor.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
taking this topic a little further ...
Q1) static block,is the one which is executed first when you run a program,why static initialiser block cannot contain any public,private or protected var's?
Q2) when a var is declared&initialised in static block it is not available to other parts of the class, so in this case static is acting like a normal method,in sense ,vars dec&ini inside it is local to that.am i right? see sample program below
Q3)this is out of this topic, is there any way to display the accessmodifier and var type of a variable ?any methods declared for this in Object or any class ?
Q4)static ini block gets executed first when a program is run, is this the same case when a static variable is declared&initialised ,i.e,does the jvm first see for static var's declared in the class ?
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by srinivas bolloju:
taking this topic a little further ...
Q1) static block,is the one which is executed first when you run a program,why static initialiser block cannot contain any public,private or protected var's?
Q2) when a var is declared&initialised in static block it is not available to other parts of the class, so in this case static is acting like a normal method,in sense ,vars dec&ini inside it is local to that.am i right? see sample program below
Q3)this is out of this topic, is there any way to display the accessmodifier and var type of a variable ?any methods declared for this in Object or any class ?
Q4)static ini block gets executed first when a program is run, is this the same case when a static variable is declared&initialised ,i.e,does the jvm first see for static var's declared in the class ?

Srinivas,
Here are the answers to your questions:
Q1) Static initializer blocks CAN "contain" (refer to) public,private or protected vars of the class itself. However:
* If specified unqualified (e.g. v = 5) then they have to be static ones; instance variables need an object to be invoked on, and because a static initializer is, well..., static, there is no 'this' reference available.
* They have to be declared before the static initializer; otherwise you'll get a forward reference error by the compiler.
Q2) If I understand the question correctly, the answer is yes.
Q3) It can be done using reflection. Here is a
sample program. Assume that we are talking about a field named f in a class named C, where the class C lies in the same package as the Tester class which contains our little test:

Compiling, running, and fixing possible bugs owing to oversight is left as an exercise to the reader.
Q4) The answer is yes. Says JLS 12.4 (Initialization of Classes and Interfaces):
"Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class".
Hope this helps,
Panagiotis.
 
Akash Kumar
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veena, Srinivas, Chung and Panagiotis
This makes sense for me . Thanks for the great help.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic