简体   繁体   中英

Not figuring out how the static variable is working in this case

I am trying to understand how the static variables work so apologies in advance if this question is basic.

I have a class Employee which has 2 public static variables both of type String and initial value null.

public class Employee {
   public static String FIRST_NAME = null;
   public static String LAST_NAME = null;
}

I have another class Job which has 1 public static variable of type Employee and initial value null.

public class Job {
   public static Employee EMPLOYEE = null;
}

I create a separate class to print out the initial values.

public class Testing {
   public static void main(String[] args) {
      System.out.println("Employee.FIRST_NAME=" + Employee.FIRST_NAME);
      System.out.println("Employee.LAST_NAME=" + Employee.LAST_NAME);
      System.out.println("Job.EMPLOYEE=" + Job.EMPLOYEE);
      System.out.println("Job.EMPLOYEE.FIRST_NAME=" + Job.EMPLOYEE.FIRST_NAME);
      System.out.println("Job.EMPLOYEE.LAST_NAME=" + Job.EMPLOYEE.LAST_NAME);
   }
}

And this was the output:

Employee.FIRST_NAME=null

Employee.LAST_NAME=null

Job.EMPLOYEE=null

Job.Employee.FIRST_NAME=null

Job.Employee.LAST_NAME=null

Then I assign values to static variables of Employee class. And then I print out all the values of both classes again.

public class Testing {
   public static void main(String[] args) {
      Employee.FIRST_NAME = "John";
      Employee.LAST_NAME = "Doe";
      System.out.println("Employee.FIRST_NAME=" + Employee.FIRST_NAME);
      System.out.println("Employee.LAST_NAME=" + Employee.LAST_NAME);
      System.out.println("Job.EMPLOYEE=" + Job.EMPLOYEE);
      System.out.println("Job.EMPLOYEE.FIRST_NAME=" + Job.EMPLOYEE.FIRST_NAME);
      System.out.println("Job.EMPLOYEE.LAST_NAME=" + Job.EMPLOYEE.LAST_NAME);
   }
}

And this was the output:

Employee.FIRST_NAME=John

Employee.LAST_NAME=Doe

Job.EMPLOYEE=null

Job.Employee.FIRST_NAME=John

Job.Employee.LAST_NAME=Doe

Why Job.EMPLOYEE is null?

Thanks for reading my question.

I hope the code was only an example. If not: you shouldn't use static variables this way, since they are global for all instances of the class in the same runtime (it's actually one variable for all instances of the class/subclasses). Job.EMPLOYEE is still null because you haven't assigned a value to it.

The variables in your classes should not be static. static means that there will only be one value for all instances of that class. ie each instance will have the same value.

You should make the variables non-static (just remove the static keyword).

You should also make them private and provide accessor methods to them such as getters and setters. You can also create a constructor that accepts a first name and last name which will create objects of the class for you.

For example:

public class Employee {

private String firstName = null;
private String lastName = null;

public Employee(String firstName, String lastName)
{
    this.firstName = firstName;
    this.lastName = lastName;
}

public String getFirstName()
{
    return firstName;
}

public void setFirstName(String firstName)
{
    this.firstName = firstName;
}
public String getLastName()
{
    return lastName ;
}
public void setLastName(String lastName)
{
    this.lastName = lastName ;
}

}

You can then create Employee objects like this:

Employee employee1 = new Employee("Joe", "Bloggs");
Employee employee2 = new Employee("John", "Smith");

You can access the object's variables like this:

System.out.println("Employee 1 first name: " + employee1.getFirstName());
System.out.println("Employee 1 last name: " + employee1.getLastName());

You can edit the object's variables like this:

employee1.setFirstName("Adam");

Then do the same for the Job class and use the Job classes setEmployee method to set a Job object's Employee variable equal to an Employee object that you have created.

Static variables are initialized the first time you access a class in your code. Here's the flow:

-Main method runs and starts at first line

-The employee class has its first name and last name variables set

-The first and second print line statements access those variables

-The employee variable is not set, so the value is null

-The Job.EMPLOYEE is null, but by calling the static variable you are calling Employee.FIRST_NAME which is static so it returns the static variable on the Employee class (not recommended as many others stated very clearly.) You may have expected a NullReferenceException, but the call you are making is on the class and not the instance.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM