Ich habe eine Mitarbeiter-, eine Organisations- und eine Testklasse. Der Mitarbeiter enthält Informationen in Bezug auf die Mitarbeiter- und die Organisationsklasse enthält die Mitarbeiterliste. Hier finden Sie Quellcode der beiden Klasse:Klassenstufe Variable Für GC in Java geeignet
Employee.java
package com.practice;
public class Employee {
private String empId;
private String name;
private int age;
private float salary;
public Employee(final String empId, final String name, final int age, final float salary) {
this.empId = empId;
this.name = name;
this.age = age;
this.salary = salary;
}
public String getEmpId() {
return empId;
}
public void setEmpId(final String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(final int age) {
this.age = age;
}
public float getSalary() {
return salary;
}
public void setSalary(final float salary) {
this.salary = salary;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.empId + " " + this.name + " " + this.age + " " + this.salary;
}
}
Organization.java
package com.practice;
import java.util.ArrayList;
import java.util.List;
public class Organization {
private final List<Employee> empList = new ArrayList<Employee>();
Organization() {
}
public void addEmployee(final Employee emp) {
this.empList.add(emp);
}
public List<Employee> getEmpList() {
return empList;
}
}
TestGC.java
package com.practice;
public class TestGC {
public static void main(final String[] args) {
final Employee emp = new Employee("E1", "Emp 1", 20, 2000.0f);
final Employee emp2 = new Employee("E1", "Emp 1", 20, 2000.0f);
final Organization org = new Organization();
org.addEmployee(emp);
org.addEmployee(emp2);
System.out.println(org.getEmpList());
}
}
In Organisation.java, haben wir eine Liste von Mitarbeiter-Objekt und ich habe das Objekt in der gleichen Zeile erstellt, das heißt auf Klassenebene. Also, meine Frage ist, wird diese Liste für GC geeignet sein, sobald ich mit Organisationsobjekt fertig bin oder es wird ein Leck im Speicher sein? Wird es auch einen Unterschied machen, wenn ich das gleiche Objekt im Konstruktor instanziiere?
Nein, es bedeutet nicht, Klassenstufe (das heißt statisch). Es ist eine normale Instanzvariable. – Kayaman
Kein Speicherleck, kein Problem. – vojta