【发布时间】:2021-07-29 07:50:51
【问题描述】:
我正在尝试将学生的数据存储在主课中,在学生课中我做了以下操作:
public class Student {
public static String name = "UNKNOWN";
Student(){
}
Student(String name) {
this.name = name;}
虽然我主要做了以下事情:
public class Main {
public static void main (String[] args) {
//s1 is short for student1
Student s1 = new Student ("Chris");
Student s2 = new Student ("Layla");
Student s3 = new Student ("Mark");
这个问题是,每当我打印一个 sx.name 时,我总是会打印最后一个。所以对于下面的代码:
System.out.println(s1.name);
我会得到 Mark,而应该是 Chris。
【问题讨论】:
-
只删除名称字段中的静态关键字
标签: java object constructor