Results 1 to 11 of 11
  1. #1
    gcampton Guest

    Default static methods, way to clear heap?

    is there a way to clear the java heap for static methods, for example.
    I create a class called arrayUtils, that I create a pile of static methods in for quick use for arrays (sort, add, delete, etc).
    Java Code:
    public static myArray[] addElement(myArray[] e)
    {
        myArray[] temp = new myArray[e.length];
        for (int i=0;i<e.length;i++)
        {
            temp[i]=e[i];
        }
        return temp;
    }
    the only problem with this is that if I use this method once, I cannot use it again because the second time round the values params and temp are the same so it does not work, being that static methods persist, calling on it a second time round does nothing. So is there anyway to clear them? or is it just best to not use them in cases such as this? Ideally what I would want is to create a static template method class/classes such as this.
    pipedream?
    Last edited by gcampton; 11-06-2009 at 03:45 PM.

  2. #2
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    I would be curious as to what problems this causes to help my own understanding. Not obvious to me, although I could see "new" failing if e.g. foo = new foo(); has set foo with a value.

    But, explicitly setting foo to null will release memory to the garbage collector.

  3. #3
    gcampton Guest

    Default

    if you are asking what i mean by failing to do anything after the first call.
    static methods persist, so lets say we call my static method addElement, it will add an element to the array, then lets say we sort the array using a typical sort method, and change some of the data, and then decide we want to add another element. calling on static method again would simply use the same variables as the first call, so potentially(i'm not 100% positive) it would change the array back to the original state before the sort and changing occurred.

    eg:
    Java Code:
    String[] myArray = new String[1];
    
    myArray[0] = "hello";
    myArray[1] = "there";
    
    for (int i=0;i<myArray.length;i++)
    {
        System.out.printf("%s ", myArray[i]);
    }
    
    // this prints out: hello there
    
    myArray = addElement(myArray);
    
    for (int i=0;i<myArray.length;i++)
    {
        System.out.printf("%s ", myArray[i]);
    }
    
    // this prints out: hello there null
    
    myArray[2]= "frank"
    
    // hello there frank, lets add another element.
    
    myArray=addElement(myArray);
    
    for (int i=0;i<myArray.length;i++)
    {
        System.out.printf("%s ", myArray[i]);
    }
    
    // prints out: hello there frank
    // i think... it seems to completely fail at the task of assignment.
    and also another wierd error is even though i have data in the last element, adding new set of data after calling addelement does not show, for some reason the old data still shows. I can show my code where I am calling it, but seems to me like it should at least rewrite the last element in the array with the new data.
    Last edited by gcampton; 11-06-2009 at 04:20 PM.

  4. #4
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    I was going to run this through a debugger to try to find any logic errors and where any assignment failed.

    Realizing that I don't have access to all of your class, I see two problems.

    You create "String[] myArray = new String[1];"

    Then you have a method
    Java Code:
     public static myArray[] addElement(myArray[] e)
    {
        myArray[] temp = new myArray[e.length];
        for (int i=0;i<e.length;i++)
        {
            temp[i]=e[i];
        }
        return temp;
    }
    I would think the routine would work if myArray were a data type, i.e. a class.
    But you have explicitly declared myArray as an ->object<- which is an array of string objects. Although you can return this value, you can't declare it as a return type. It is merely a variable which references an array of strings.

    The other problem I'm seeing is the line

    myArray[] temp = new myArray[e.length];

    Besides the repeated confusion about declaring temp as a datatype of myArray, you are merely creating an array of the same length as the old array. What you are doing is merely copying the old array into a new array. This line should be
    myArray[] temp = new myArray[e.length + 1];

    But you indicate that it works (although the line is commented). While I'm not working with all the code, I'm surprised that you got this to compile.

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    A few minutes after my last post:

    I rewrote your add element as:
    Java Code:
    public static String[] addElement(String [] e)
    	{
    	    String [] temp = new String [e.length + 1];
    	    for (int i=0;i<e.length;i++)
    	    {
    	        temp[i]=e[i];
    	    }
    	    return temp;
    	}
    and merely added it as a subroutine in the application class rather than as a class method.

    Then your posted code blew up at myArray[1] = "there" because you referenced the second element in the array which only has one element -> array out of bounds error. EDIT: Likewise, myArray[2] = "Frank" should have caused the same error.
    Last edited by rdtindsm; 11-06-2009 at 07:40 PM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Static methods (or instance methods) do not persist. They don't live on the heap either.
    It's not surprising then that I am completely at a loss as to what this thread is about.

  7. #7
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    I agree that the OP didn't have the correct understanding of static methods.

    I do have enough background to understand how things were stored way back in the DOS days and had to work with file headers for .exe files, and we may be getting into a discussion that is beyond what most students would know. The following extrapolates from general knowledge rather than specific knowledge, and is presented so that we can all clarify the subject.

    In DOS two areas of memory were assigned when an executable is loaded. One is a data area which contains "static variables", and there is an area of memory for program code. The non-stack data memory typically contains the actual string data which is referenced by a pointer, and I would think it is the area where static or global variables are stored. Actually, there is a third area of memory assigned for the stack which is used for local variables, calling parameters, and return values.

    In C/C++ the process equivalent to String foo = "some string" as a local variable would put "some string" in the data area, and foo would be a pointer (reference) on the stack.

    Technically, the heap is unassigned memory and doesn't contain any valid data. When memory is allocated, as in calling a constructor - new foo(), the memory is allocated from the heap. Personally, I tend to lump all memory outside the stack as heap. I can see someone else making the same association.

    What is relevant to the OP is that it is not the persistence of the method, but the management of data that is the problem.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by rdtindsm View Post
    ...

    What is relevant to the OP is that it is not the persistence of the method, but the management of data that is the problem.
    They are using their perception of the persistence to decide how to manage the data.

  9. #9
    gcampton Guest

    Default

    ok I simply tried to find why these methods as static would not add elements where as normal methods they do. I probably could have worded it better, and wrote sample code better too but was late @ night.

    But your right in the fact I got NFI what static methods are and how they work, but I have read on numerous posts that they "persist". The meaning I took from this is that the variables do not reset.

    Java Code:
    public class arrayUtils
    {
      public static Fruit[] addElements(Fruit[] a)
      {
        Fruit[] newArray = new Fruit[a.length];
        for (int i=0;i<a.length;i++)
        {
          newArray[i]=a[i];
        }
        return newArray[i];
      }
    
      public void addNewFruit()
      {
        String name, color;
        Scanner input = new Scanner(System.in);
        System.out.printf("Enter new fruit name.");
        name=input.next();
        System.out.printf("Enter fruit color.");
        color=input.next();
        
        FruitDriver.myFruit=addElements(FruitDriver.myFruit);
    
        FruitDriver.myFruit[FruitDriver.myFruit.length-1] = new Fruit(name, color);
      }
    }
    Java Code:
    public class FruitDriver
    {
      public static Fruit[] myFruit = new Fruit[0];
      public void fruitDriver()
      {
        arrayUtils lah = new arrayUtils();
        lah.addNewFruit();
    }
    // fruit class is typical (string name, string color) for testing purposes.
    This is pretty much exactly my code, except I'm working with different types of objects with multiple type params. As addElement being static, after I add the first element and store the object in that array element calling on that method a second time does nothing. Whereas when I simply delete the 'static' context it works as intended. I changed the static content from addNewFruit, which seems to fix this problem...

    I just don't understand why if it is static, it seems to keep the same data, where as if not static it works as intended. AddElement is still static, but addNewFruit has been changed to nonstatic to work.

  10. #10
    gcampton Guest

    Default

    ok ignore me, I just changed everything back to static to retest and it works fine, so god knows what I was doing last night at 1am when it kept braking... perhaps I left static from addNewObj or something silly.

    As far as the difference between the 2, I know that a: it's a lot easier to call on static methods as you don't need to create an object of them, and b: only 1 static method can exist at a time.

    But other than that...
    Last edited by gcampton; 11-07-2009 at 02:49 AM.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by gcampton View Post
    ..
    As far as the difference between the 2, I know that a: it's a lot easier to call on static methods as you don't need to create an object of them,..
    It is but they should only be used for utility methods. In OOP, actions are part of an object's behavior and so you must have an instance of an object to be able to perform an object action. You must therefore use instance (non-static) methods for object actions and use static methods for utility functions that cannot be tied to an object.



    Quote Originally Posted by gcampton View Post
    .. b: only 1 static method can exist at a time.

    But other than that...
    You can have many static methods in a class. You can also have many static fields in a class. The point is that if you do have a static field (not method) then all object instances created from that class will share that same static field.

    All these things are well explained in Sun's Java tutorial.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM
  3. Static methods
    By carderne in forum New To Java
    Replies: 10
    Last Post: 01-03-2009, 05:27 PM
  4. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •