Jump to content

What do these words mean???

- - - - -

  • Please log in to reply
3 replies to this topic

#1
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Hello dear friends! Can anyone explain me exactly what the followings in java do? what they mean?

Garbage collector (gc)
Finalizer
Static
Overloading

Also, i heard that gc and finalizer are important things. Are they? Must we use it?
thanks in advance.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Garbage collector
The java virtual machine cleans up after you.
If you make objects, and later don't use them anymore, the garbage collector will get rid of it to free up memory and in this way prevent memory leaks which cause your memory to fill up gradually and eventually the memory is full and the program stops.
The garbage collector knows you don't use objects anymore once all references to that object are gone.
public class MyClass{

  private String word;

 

  public MyClass(){

    word = "hi";

  }

  public void deleteWord(){

    word = "hey";

  } 

}
When you create an object of MyClass it also creates a String object in the memory, and 'word' points to that String object.
When you use deleteWord, word won't point to "hi" anymore, but to a new String "hey". The "hi" String is now still in the memory tho.

In other languages like C you have to specificly say <delete "hi">, in order to free up the memory. In Java you just have to wait untill the garbage collector passes by, notices nothing is pointing at "hi" anymore, and then it gets rid of "hi" to free up memory.

Finalizer
A finalizer is a method (finalize() ) which is called before the garbage collector removes the object from the memory. If for example you have an open file in an object. You could close the file properly in the finalize() method. When you then get rid of this object, garbage collector notices nothing is using it anymore, it should call finalize() first.

This is actually a bad way. It's often better to manually call a method to close the file yourself, because it's the garbage collector that invokes finalize(), you , as programmer, have no control over when the finalize() method is called. If it's even called at all. Maybe the garbage collector hasn't been there yet, and you exit the program. That could possibly result in corrupt files if they weren't properly closed.
Use with caution.

Static
Anything that is static, can be used without needing an object. They also often are final, meaning they can't be changed once they are set. It doesn't have to be final tho.
example:
public class Hummer{

  [B]public static final int NUMBER_OF_WHEELS = 4;[/B]

  public Color color;

}


public class OtherClass{

  public static void main(String[] args){

     System.out.println("Hummer has " + [B]Hummer[/B].NUMBER_OF_WHEELS + " wheels.");

  }

}
I could access the number of wheels without needing to create a new object.
If i wanted the non-static color i need to create an object first:

public class Hummer{

  public static final int NUMBER_OF_WHEELS = 4;

  [B]public Color color[/B];

}


public class OtherClass{

  public static void main(String[] args){

     Hummer [B][COLOR="orange"]aHummer[/COLOR][/B] [B]= new Hummer();[/B]

     System.out.println("Hummer is painted " + [B][COLOR="orange"]aHummer[/COLOR][/B].color + ".");

  }

}

Note that using public attributes is actually a bad design, except for the statics.

Methods can also be static:
public class Hummer{

  public static final int NUMBER_OF_WHEELS = 4;

  public Color color;


  [B]public static void printInfo()[/B]{

    System.out.println("Hummer was a brand of trucks marketed since 1992 by AM General when it began selling a civilian version of the M998 Humvee");

  } 

}


public class OtherClass{

  public static void main(String[] args){

     Hummer.[B]printInfo[/B]();

  }

}



Overloading
Overloading is using the same method name in a single class, but with different parameter lists.
public class Hummer{

  private Color color;


  public Hummer(){

    color = Color.GREEN;

  }


  public Hummer(Color color){

     this.color = color;

  }

}
Here i've overloaded the constructor. (note how green is a static variable from the Color class).
If i want to change the color,i could either provide a color, or the red, green, blue values, so i can also overload the changeColor method:

public class Hummer{

  private Color color;


  public Hummer(){

    color = Color.GREEN;

  }


  public Hummer(Color color){

     this.color = color;

  }


  public void changeColor(Color color){

    this.color = color;

  }


  public void changeColor(int red, int green, int blue){

    color = new Color(red, green, blue);

  }

}



#3
Chunes

Chunes

    Newbie

  • Members
  • Pip
  • 2 posts
Also, if an instance variable is static, then it is shared by all objects of that class. No matter how many objects you create, they all use only that one variable. So if you change the value, you change it for all objects. It's most often useful for values that don't change, however. It's a nice way to save memory.

#4
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
thanks friends! Now i know these words :c-smile:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users