Question about variables inside methods

I just want to be sure that I'm clear on something.
I have a method that is called inside a class, the local variables
get initialized with my initial values.
When the method exits, those local variables get marked with
a zero reference count and are available for GC when the garbage
collector runs.
Correct?
Thanks.
-brad w.

when the method destroy,local variable destroy too .In the stack,when stack.pop(),the reference to the object still exists , though object has been removed.according to this , memory leak will appear .SO we should do something like this.
Stack stack=new Stack(1000);
for(int a=0;a<1000;a++)
stack.push(new Integer(a));
for(int a=0;a<1000;a++)
System.out.println(stack.pop());
public Object pop(){
if(size==0)
throw new EmptyStackException();
Object object=elements[--size];
elements[size]=null; //eliminate  the reference out of life
return object;
/code]
affter that ,the garbage collector can do something what he want to do                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Similar Messages

  • A question about the getProperty method defined in the Security class

    Hello Everyone!
    I would like to ask a question about the getProperty method defined in the
    Security class.
    public static String getProperty(String key) Do you know how can I exract the list of all possible keys?.
    Thanks in advance,

    I found the answer, in fact the keys are defined in the java.security file.

  • Question about variable declaration

    Hi all,
    I have a (in fact, two) question(s) about the declaration of variables inside of time-critical code, like render loops or similars.
    Does it really matter to use
    for (int k=0; k<1000; k++)
         for (int l=0; l<1000; l++)instead of
    int k;
    int l;
    for (k=0; k<1000; k++)
         for (l=0; l<1000; l++)concerning the speed of the app? What about doing this with non-basic types -> Objects, like to say Strings ?
    And are there any resources where I can find out more about that and other things like that ?
    Uhm, now it became three q's... Sorry to bother you ;-)
    Skippy

    whoo, maybe I got something totally wrong here.
    Does this mean that
    Image blah;
    for (int i=0; i<100; i++) blah = getImage("test"+i+".gif");will clean up a lot, because there are non-referenced Images and Strings?
    Hmm, anyone told me that
    for (int i = 0; i<100; i++)
    String test = "test"+i;
    System.out.println(test);
    }is disastrous code.
    Damn, ever thought to be a good Java programmer, but every day I get new thoughts I cannot solve these days...
    grmph
    Skippy

  • Question about struts inside javascript variable

    Hi, actually i'm using a struts + javascript to create HTML code that i use inside a popup window...
    My (working) code is:
              idoc="";
              /* var cols = parent.document.getElementById('rowA').getElementsByTagName('THEAD')[0].getElementsByTagName('TH'); */
              var idoc='<html:form styleId="InsertNewItem" action="/nw/insertnewitem.do?target='+parent.newPop.action+'">';
              idoc+='<TABLE><TBODY>';
              var hiddenForm = getParentObj('hidden_data_form');
              var cols = hiddenForm.getElementsByTagName('INPUT');
              var test;
              test="cippa";
              for(k = 0; k < cols.length; k++) {     
                   if(cols[k].id.substring(0,3) == "hh_")
                        idoc += "<TR><TD>";
                        idoc += cols[k].id.substring(3);
                        idoc += "</TD><TD>";
                        idoc += '<html:text property="'+cols[k].id+'" value=""/>';
                        idoc += "</TD></TR>";
              idoc+='</TBODY></TABLE>';
              idoc+='</html:form>';
              var towr = document.getElementById("subwin_body");
              towr.innerHTML = idoc;
    The problem is when i try to add a <html:select> to the code, more precisely i added just before the end of the table these lines:
              idoc += "<TR><TD>Ditta:";
              idoc += "</TD><TD>";
              idoc += '<html:select property="ditta_id">';
              idoc += '<html:options collection="sel_ditta_list" labelProperty="label" property="value"/>';
              idoc += '</html:select>';
              idoc += "</TD></TR>";          
    ... but i cannot get it to work because struts seems to make some mistake with quotes and in javascript console window i get this error:
    Error: unterminated string literal
    File source: http://localhost:8180/nw/jsp/insertNewItemOperatore.jsp
    Row: 121, Column: 10
    Source Code:
    idoc += '<option value="1">1</option>
    (note the missing ending single-quote)
    I can't understand why because with <html:text> field the same "trick" works fine... any ideas?
    Thanks in advance
    Luca
    Message was edited by:
    spallared

    can you post all of your source code for the .jsp and then try running the page and once it fails do a View Source and then paste in all the generated code so that we can look at that as well.
    i'm not totally positive but i have a pretty good feeling what is going wrong. the struts optionsCollection tag will render a bunch of <option> elements but print a newline after each one, which means you probably end up with something like
    var whatever = '<option ...>.........<option>
    <option ...>.........<option>
    <option ...>.........<option>
    <option ...>.........<option>';
    correct? if so, that is not valid javascript.
    Message was edited by:
    den2681

  • Question about variable scope

    Suppose theres a class that mixes colors.
    It has an constructor and two methods:
    public class ColorMix {
    //This is just an example but I've tried this and colors
    mix and show up correctly.
    public ColorMix() {
    cmColor = MixColors();
    } //e_constr
    private void mixColors() {
    //do something
    Color1 = createColors();
    Color2 = createColors();
    //etc
    private Color createColors() {
    //create some color-values
    //r, g and b ...
    Color anColor = new Color (r, g, b) //finally create color
    return anColor; //and return that color
    public Color cmColor;
    } // e_class_ColorMix
    My question is: Howcome and how the variable
    'anColor' is referred from the createColors-method to
    the mixColors-method?!? I thought that anColor would
    be out of scope, since its created in the createColors
    I've understood it would be erased once createColors
    is done with?!?
    Or did MixColors make it to the 'anColor' before garbage
    collector?
    Thanks,
    Jani

    I think you are mixing up variables and objects. A variable (like "anColor" in your example) is not the same as an object. A variable is just a reference to an object that exists somewhere in memory.
    Objects are eligible for garbage collection if the running program does not hold any reference to the object anymore.
    Your createColors() method creates a new object of type Color and stores a reference to it in the variable anColor. Then it returns the value of the variable (which is the reference to the Color object) to the calling method: mixColors. There you have another variable (Color1) which will hold the reference to the object.
    So the variable anColor goes out of scope at the end of method createColors(), but the program still has a reference to the object, stored in variable Color1.
    The object is still referenced by the program and will therefore not be garbage collected.
    I'd suggest you read the Java Tutorial: http://java.sun.com/docs/books/tutorial/java/index.html
    Jesper

  • Question about synchronization the methods that work with Properties

    Hello everyone
    I have the following problem with the properties file and synchronization.
    I read properties after the programm was started. And If the properties exist the programm load the properties (java.util.Properties) from the properties file.
    Than I have make the possibility to change this properties in GUI and save it. That mean that the properties file will be used from two plases: the first at the beginning and the second in GUI.
    I wrote the wrapper for properties, say ApplicationProperties.
    I defined ApplicationProperties as the Singleton. At this file I defined
    setters/getters for my variables. This class has also two methods
    load properties or save properties. This two methods work with the propertiesFile the instance of java.io.File.
    The first reads the properties with FileInputStream and the second save it to this file with the FileOutpuStream.
    My question is, do I need to synchronize this two methods to make it thread save?
    private File propertiesFile;
    private void loadPropertiesFromFile() throws FileNotFoundException, IOException {
         FileInputStream input = null;
         try {
              input = new FileInputStream(this.propertiesFile);
              this.properties.load(input);
              updateSettingsFromProperties();
         } catch (FileNotFoundException fnfE) {
              throw new FileNotFoundException("Cannot find the properties file.");
         } catch (IOException ioE) {
              throw new IOException("Cannot read properties file.");
         } finally {
              if(input != null) {
                   input.close();
    private void savePropertiesToFile() throws IOException {
         FileOutputStream out = null;
         try {
              out = new FileOutputStream(this.propertiesFile);
              this.properties.save(out, PROPERTIES_DESCRIPTION);
         } catch (IOException io) {
              throw new IOException("Can't save properties.");     
         } finally {
              if(out != null) {
                   out.close();     
    }Thanks a lot for your advise!

    Will you ever be reading or writing the same file from multiple threads at once?
    Synchronisation can prevent this from happen, but does it ever happen at the moment?

  • Question about variable names

    k, so I have a for() loop that I want to take the value of String fs1 through String fs21 and add them in that order to a String fs. So, my question is, can you do this in a loop? you can do it in PHP, and that's what I'm accustom to.
    this is kinda what I want to make
    String fs = "";
    String fs1 = "foo";
    String fs21 = "bar";
    for(int i = 0; i<22;i++)
         fs+= (the variable "fs" plus the number of "i", so like fs1, fs2, fs3. . . fs21);
    System.out.println(fs);

    I think you want to use and array and be careful about using += that way.
    StringBuffer buffer = new StringBuffer();
    String[] fs = new String[2];
    String fs[0] = "foo";
    String fs[1] = "bar";
    for(int i = 0; i<22;i++)
         buffer.append(fs);
    return buffer.toString();

  • Question about NAT Inside Source, Inside Destination, and Outside Source

    I read the Cisco command references about "ip nat inside source", "inside destination", and "outside source", but couldn't have a clear understanding of how to associate the commands with "ip nat inside" and ip nat outside" configured for interfaces.
    Does "ip nat inside source ..." translation only happen on the interface configured as "ip nat inside"?
    Since NAT is a bidirectional action, what's the difference between "ip nat inside cource ..." and "ip nat inside destination ..."?
    I've never used "ip nat outside source ...". In what cases would it be needed?
    On an interface where there are NAT translation and also other actions such as policy map or IP Sec crypto map, would NAT happen before or after other actions?
    Thanks for help with any questions.
    Gary

    Hi Gary,
    The following documents may help you to understand some of the terminology:
    http://www.cisco.com/en/US/customer/tech/tk648/tk361/technologies_tech_note09186a0080094831.shtml
    http://www.cisco.com/en/US/customer/tech/tk648/tk361/technologies_tech_note09186a0080094837.shtml
    Also, the following document has a clear explanation of the order of operations when using NAT:
    http://www.cisco.com/en/US/customer/tech/tk648/tk361/technologies_tech_note09186a0080133ddd.shtml
    Hope that helps - pls rate the post if it does.
    Paresh

  • Question about expressions inside AE

    I don't even know what to search for on google to find the answer. So after an hour trial and error inside AE and failure in google search, I'm coming here to seek help.
    I have a plugin (RedGiant Sound Keys) that is giving values as keyframes for every frame. And the values go like: 0, 4, 7, 3, 5, 0, 0, 5, 6 ....
    In other words, they fluctuate between zero and, say, 10.
    I want another layer to get this value, but add it to itself every frame. I used to do QBasic programming and it was very simple there. Just something like x = x + (value from the Sound Key plugin)
    But I don't know how to do this in AE.
    Right now I have this:
    x = (thisComp.layer("sound").effect("Sound Keys")("Output 1") * 0.5);
    x;
    What I really want is:
    x = x + (thisComp.layer("sound").effect("Sound Keys")("Output 1") * 0.5);
    x;
    But it doesn't work.
    Can someone help me please?

    AE does not store persistent temporal variables. you have to accumulate them manually and write your own loops.
    Expression Speed and Frequency Control
    Soundkeys has an accumulate mode built-in, also, so rather than put up with complex expressions, simply use a duplicate instance of the effect and tweak the values.
    Mylenium

  • Very easy question about variables

    Can I convert float-variable to String-variable?

    Hi, a very easy way to convert almost anything is
    called "typecasting". It might sound difficult, but
    it isn't.
    You use it like this example
    > String outcome;
    Double input;
    input = 3.54789654587;
    Output = ""+input;
    That's not casting. It's implicit formatting.
    What it does is making an empty string and pasting
    your number behind it. VERY easy! You can do this
    with allmost every type of variables.What it's doing is creating a StringBuffer, appending an empty string to it, then appending the result of calling toString on the Double, which is itself a String. (Though things change a bit in jdk1.5.) It works because StringBuffer.append has versions for each primitive type, and toString exists for all Objects.
    One problem is that it's not efficient -- you're doing a lot of work behind the scenes to do something that can be done much more simply. But the bigger problem is that it's using operators in a roundabout way and with implicit results. So it's sort of a hack. "+" is the append operator for Strings (well it actually comes down to an implicit method call, or several). Putting the double on the right side of that also causes an implicit format. So you're implicitly formatting a value, and then appending it to nothing, to get a result. You're not actually meaning to append, you're meaning to format, but you're using an append operator.
    The more straightforward way to do it is simply to explicitly format it, using the methods already shown on this thread, or using java.text.DecimalFormat.

  • Question about variable copying, referencing and LinkedList constructors.

    Is there any advantage in this recommendation?
    "In general, do make sure that you copy mutable arguments and mutable return values when appropriate."
    Now, according to this suggestion:
    public ClassName(LinkedList<String> list)
         this.list = new LinkedList<String>(list);
    Instead of public className(LinkedList<String> list)
         this.list = list;
    }I am confused about the situation where using the copy constructor would be appropriate as opposed to the second method.
    Can someone please explain which would be an ideal situation to use a copy constructor and when to use mere assignment?

    fantastic_ray wrote:
    Is there any advantage in this recommendation?
    "In general, do make sure that you copy mutable arguments and mutable return values when appropriate."If you don't copy mutable arguments and return values, then when you change the state ("contents") of the object, it will be seen by both the original reference and the copy. Sometimes you may want this. Sometimes you may know that no change will occur. You have to judge what's appropriate for your use. In general, though, if there's a chance the elments' states may change, you'll want to copy them.
    >
    Now, according to this suggestion:
    public ClassName(LinkedList<String> list)
         this.list = new LinkedList<String>(list);
    Instead of public className(LinkedList<String> list)
         this.list = list;
    It depends. After you do new ClassName(list), if the code that created the ClassName object is going to keep using the list and you don't want any changes it makes to be seen by the new object and vice versa, then yes, you'd want to do the former.

  • A question about variables and where I should put them!

    Hi, I'm fairly new to java. I'm a first year computer scientist and I'm struggling with where I should put my variables.
    So far on the course I've been able to wing it as we have not been doing object orientated programming, it was more like programming in fortran!
    Anyway if you look at the code below, I need the variables 'speed', 'distance', 'time' and 'tottime', to be calculated in my seperate methods, but then called in my main part to be printed out; however I have no idea where I put them to get this to work!
    The program is by no means complete, it's still a work-in-progress, but I need to get this sorted before I continue.
    Thanks in advance
    /////  LogName: gxm07u
    /////  FullName:
    /////  CreationDate: 2007-11-07 09:30:07
    import java.io.*;
    public class travel {
        public static void printMenu()
             System.out.println("Travel Program\n\nn: Enter a journey\ns: Get complete journey stats\nd: Get Distance travelled stats\nq: quit");
        public static void RoadMenu()
             System.out.println("Type the road type:\nType m: Motorway\nType a: A road\nType b: B road\nType u: Urban road");
        public static void enterRoadType()
             char choice;
             choice = UserInput.readChar();
             switch(choice)
                  case 'm': //motorway
                  case 'M':
                       speed = 85;
                  break;
                  case 'a': // a road
                  case 'A':
                       speed = 70;
                  break;
                  case 'b': //b road
                  case 'B':
                       speed = 55;
                  break;
                  case 'u': // Urban road
                  case 'U':                                     
                       speed = 40;
                  break;     
        public static void enterDistance()
             System.out.println("Type the travel distance: ");
             distance = UserInput.readDouble();
        public static void calcTime()
             time = speed/distance; // time in hours
             tottime = tottime + time; //total time in hours         
        public static void main(String[] argv) {
              int speed = 0;
              double distance = 0;
              double time=0, tottime=0;
         } // end of main
    } // end class

    Your calcTime() can't see variables local to other methods you either need to make them global or pass them in as parameters. To make them global:
    public class travel {
         int speed = 0;
         double distance = 0;
         double time=0, tottime=0;
        public static void printMenu()also you're main method should create a instance of the class
        public static void main(String[] argv) {
    new travel();
    }BTW it's convention to make class names begin with a uppercase letter. Also, look at using the equalsIgnoreCase() for your switch statement as it could save you 4 cases.
    Hope this helps

  • Noob Question about Variables

    Hello,
    I have an element called "element1" and inside it is another
    element called "element2"
    main
    └element1(contains variable "var")
    └element2
    note:both elements are movieclips, not buttons
    As shown in my diagram, variable "var" is set up inside
    element1's actionscript
    and I want to be able to control "var" from element2
    I could use _global. but I don't want to control all the
    variables named "var" in my file
    what should I do?

    _global is not in AS3, so you wouldn't be able to use it
    anyways. And var is a reserved term so you'd be hitting a wall
    there too. If element2 is contained within element1, then to affect
    the var in element1 from element2 you would use:
    MovieClip(this.parent).variable = whatever

  • Question about variables in TEXTCOLUMNS !!!HELP

    Hi friends,
    can somebody tell me if it is possible to put varaibles in textcolumns(VARCHAR2)
    like 'Das Fondsvolumen des Fonds betrdgt <Fondsvolumen>' (Fondsvolumen ist the variable--> which is a column of another table in the DB)
    into the database which will be evaluated when you query the column with the text????
    Is that possible and if yes how????
    thanxx
    Schobbi

    Schobbi,
    You could use a view for the
    selection part to replace
    your variables with the concrete
    values. Write a function that returns
    those values/the whole string and use
    it in this view, if you like.
    Hope this helps,
    Karsten

  • Applescript question about variables

    I have a command in AppleScript that goes like this
                                  set focus check box "/app/con[0]/ses[0]/wnd[0]/usr/chk[1,12]"
    I would like to make the "12" a variable, and pass a new value by putting it in a loop
    Something like
                                  set focus check box "/app/con[0]/ses[0]/wnd[0]/usr/chk[1,x]"
    Any suggestions to how to pass variable x in a string?
    Thank you for your time

    "/app/con[0]/ses[0]/wnd[0]/usr/chk[1," & x & "]"
    Proof:
    set x to 12
    "/app/con[0]/ses[0]/wnd[0]/usr/chk[1," & x & "]" = "/app/con[0]/ses[0]/wnd[0]/usr/chk[1,12]" --> true
    Message was edited by: Pierre L.

Maybe you are looking for

  • Pointbase : How can I create a stored procedure with Pointbase database?

    Hello, Excuse me for my english, I'm not anglophone. I try to create a stored procedure. This is my file SampleExternalMethods.java :   import java.sql.*;    //import com.pointbase.jdbc.jdbcInOutDoubleWrapper;          public class SampleExternalMeth

  • Inserting records using DB link

    Hi, I'm trying to insert a record using a db_link into a table which has EXTRAFIELDSTYPE column and I'm getting the error PLS-00453: remote operations not permitted on object tables or user-defined type columns. Is there anyway I can insert records i

  • Out put type

    I get the above error in VA01, when trying to print preview a sales order. The master data, output procedure and determination look okay. The Smartform gets determined, but I canu2019t print preview. It is based on the sp partner, and that is determi

  • Speeding up access to local workstation files

    I've published a RemoteApp (Excel 2010) on our Server 2012 R2 RDS machine. Users are opening files on their local computers from within the Excel RemoteApp, but every time they go to File --> Open, it takes the server a long time - several minutes in

  • F drive error

    when trying to install the latest version of iTunes, i get a F drive error. anyone know what to do? i can't register/use my new iPod touch with the latest version