【问题标题】:Why can't I declare object in my local class in Java?为什么我不能在 Java 的本地类中声明对象?
【发布时间】:2022-11-17 04:24:23
【问题描述】:

我最近开始学习编程。我知道它看起来毫无意义,但我想知道为什么我不能在本地类中声明我的对象。我可以在我创建的第二个类中声明它,但不能在主类中声明。

这是我的代码:

public class User {
    
 public String name;
 public String surname;
 public int age;
 public static String companyname;
 
 User student = new User();
 student.name = "Jack";
}

【问题讨论】:

    标签: java eclipse


    【解决方案1】:

    首先,我想通过说您不能在 User 类中递归声明 User 的实例来打开我的答案,因为这会在创建新的 User 时溢出堆栈。事实上,User 的创建会触发另一个 User 的创建,而后者又会生成另一个 User 等等,从而导致无穷无尽的 User 实例化链。这是一个逻辑错误。

    此外,您的 IDE 可能会向您发出另一个错误信号,一个语法错误,关于该语句:

    student.name = "Jack";

    尽管逻辑上不正确,但在语法上允许您在 User 类中声明 User,就像您在这里所做的那样

    User student = new User();

    这将被解释为您的 User 类的属性。但是,您不能在块外编写任何逻辑。如果您想将名称“Jack”分配给您的学生变量,您有以下三种选择:

    1. 定义一个构造函数,它接受名称并在实例化期间将值 Jack 传递给您的学生实例。
      public class User {
      
          public String name;
          public String surname;
          public int age;
          public static String companyname;
      
          //Let's pretend this is fine
          User student = new User("Jack");
          
          public User(String name){
              this.name = name;
          }
      }
      
      1. 在实例块中包含您的语句。
      public class User {
      
          public String name;
          public String surname;
          public int age;
          public static String companyname;
      
          //Let's pretend this is fine
          User student = new User();
      
          {
              student.name = "Jack";
          }
      }
      
      1. 在类构造函数中初始化您的学生字段。
      public class User {
      
          public String name;
          public String surname;
          public int age;
          public static String companyname;
      
          //Let's pretend this is fine
          User student = new User();
      
          public User(){
              this.student.name = "Jack";
          }
      }
      

      但是,所有这些选项都没有什么意义,它们都是为了让您了解可以编写语句的上下文。

      我假设您只是想用它的实际字段(namesurnameagecompanyname)定义一个 User,然后在“测试”上下文中声明一个 Student,例如 main方法,来测试你的类。这很可能是您尝试执行的操作:

      public class User {
      
          public String name;
          public String surname;
          public int age;
          
          public static String companyname;
      
          public User(String name, String surname, int age) {
              this.name = name;
              this.surname = surname;
              this.age = age;
          }
      
          public static void main(String[] args) {
              User student = new User("Jack", "Black", 15);
          }
      }
      

      另外,您应该将类​​的属性(其字段)定义为privatepublic 访问修饰符通常用于对象(其方法)提供的服务。这也称为 information hidingencapsulation。基本上,您想隐藏类的内部实现并保护其状态(其字段)免受任何滥用,例如分配不一致的值。您的方法是调节对内部状态的访问的“外层”,它们包含防止误用和对对象状态进行不一致更改的逻辑。

      无封装

      public class User {
      
          public String name;
          public String surname;
          public int age;
         
          public User(String name, String surname, int age) {
              this.name = name;
              this.surname = surname;
              this.age = age;
          }
      
          public static void main(String[] args) {
              User student = new User("Jack", "Black", 15);
              
              //misuse of the object's attributes
              student.age = -20;
          }
      }
      

      封装

      public class User {
      
          private String name;
          private String surname;
          private int age;
      
          public User(String name, String surname, int age) {
              this.name = name;
              this.surname = surname;
              this.age = age;
          }
      
          public void setAge(int age) {
              if (age < 0 || age > 120) {
                  return;
              }
              this.age = age;
          }
      
          public int getAge() {
              return age;
          }
      
          public static void main(String[] args) {
              User student = new User("Jack", "Black", 15);
      
              student.age = -20; //gives you an error, you cannot access anymore this field directly
      
              student.setAge(-20); //It won't update its age to -20
              System.out.println(student.getAge()); //It still prints 15
      
              student.setAge(25); //Regulates the update and allows the assignment
              System.out.println(student.getAge()); //Prints 25
          }
      }
      

      这篇文章很好地解释了封装的概念:

      https://www.geeksforgeeks.org/encapsulation-in-java/

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 2011-01-09
      • 2011-04-04
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多