aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String" Watch "String" New topic
Author

String

Akash Kumar
Greenhorn

Joined: Jul 17, 2002
Posts: 13
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

Joined: Mar 22, 2002
Posts: 1055

I tested this code and I got this:
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Jun 20, 2002
Posts: 442
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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Chung Huang
Ranch Hand

Joined: Jun 21, 2002
Posts: 56
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.


Let us be showered in the light of confusion!
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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 ?


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
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

Joined: Jul 17, 2002
Posts: 13
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
 
subject: String
 
Similar Threads
StringBuffer question
String Buffer
EQUALS method
equals method and StringBuffer
internal doubt abou string and string buffer