class Employee {
    private String last;
    private String first;
    private String title;
    private int age;

    static int count = 2;




    public Employee() {++count;}
    public Employee(String last, String first,
                    String title, int age)
    {
        this.last = last;
        this.first = first;
        this.title = title;
        this.age = age;
    }
    public String getLast() {
        return last;
    }
    public String getFirst() {
        return first;
    }
    public String getTitle() {
        return title;
    }
    public int getAge() {
        return age;
    }
    public void setLast(String last) {
        this.last = last;
    }
    public void setFirst(String first) {
        this.first = first;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String toString() {
        return "{" + last + "," + first +
               "," + title+ "," + age + "}";
    }

    public static int getCount() {
        return count;
    }

   protected void finalize() {
        count--;
    }
}

public class  Trial {
    public static void main(String[] args)
    {
        Employee e0 = new Employee();
        Employee e = new Employee("Malone", "Karl", "Forward", 36);
        System.out.println(e);
        System.out.println("Employee.count == " + Employee.getCount());
        e0 = null;
        System.gc();


        System.out.println("Employee.count == " + Employee.getCount());
        e = null;
        System.gc();
        System.out.println("Employee.count == " + Employee.getCount());
    }
}

(java 6)
when i run it the nb of object doesnt change
finalize()doesnt affect static attribute???
why??

Recommended Answers

All 3 Replies

there's never a reason to use finalize(), at least not until you know very well how it works and once you do you realise there's always a better way to do things.

Finalize works on an instance of a class, if it works at all. Static members are not bound to any instance and therefore aren't finalized until such a time as finalize is called on the Class instance for the class as a whole, which typically doesn't happen until the JVM shuts down.

10x for u help jwenting..

if i can bother u with another question..
in c++ we use to make a default value constructor..that help me to create an object with differents arguments in the main function.

that kind of constuctor exists in java????

You can create pretty much any constructor style you want in Java.
The only thing not done so much is a copy constructor, as Java has special mechanisms for that functionality using the Cloneable interface (but you could still do it).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.