Java实例变量声明和初始化在两个语句中

发布于 2024-09-18 13:49:55 字数 306 浏览 2 评论 0原文

您好,我在 java 中初始化时遇到问题,以下代码给我编译错误,名为:expected instanceInt = 100;但我已经宣布了。如果这些事情与堆栈和堆相关,请用简单的术语解释,我是java新手,我对这些领域没有高级知识


public class Init { 

int instanceInt;  
instanceInt = 100;

   public static void main(String[] args) {

     int localInt;
     u = 9000;
     }

}  

Hi I'm having problem with initialization in java, following code give me compile error called : expected instanceInt = 100; but already I have declared it. If these things relate with stack and heap stuff please explain with simple terms and I'm newbie to java and I have no advanced knowledge on those area


public class Init { 

int instanceInt;  
instanceInt = 100;

   public static void main(String[] args) {

     int localInt;
     u = 9000;
     }

}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

强辩 2024-09-25 13:49:55

您不能在课堂中间使用语句。它必须位于一个块中或与您的声明位于同一行。


执行您想要的操作的常用方法是:

  • 声明期间的初始化

    公共类MyClass{
        私有 int i = 0;
    }
    

    如果您想定义字段的默认值,通常这是一个好主意。

  • 构造函数块中的初始化

    公共类MyClass{
        私有 int 我;
        公共我的类(){
            这个.i = 0;
        }
    }
    

    如果您想在字段初始化期间有一些逻辑(if/循环),可以使用此块。它的问题是,要么你的构造函数会互相调用,要么它们都具有基本相同的内容。
    对于您的情况,我认为这是最好的方法。

  • 方法块中的初始化

    公共类MyClass{
        私有 int 我;
        公共无效setI(int i){
            这个.i = i;
        }
    }
    

    这并不是真正的初始化,但您可以随时设置您的值。

  • 实例初始化块中的初始化

    公共类MyClass{
        私有 int 我;
        {
             我 = 0;
        }
    }
    

    当构造函数不够时使用这种方式(请参阅构造函数块上的注释),但通常开发人员倾向于避免这种形式。


资源:

关于同一主题:


奖励:

这段代码的作用是什么?

public class MyClass {
    public MyClass() {
        System.out.println("1 - Constructor with no parameters");
    }

    {
        System.out.println("2 - Initializer block");
    }

    public MyClass(int i) {
        this();
        System.out.println("3 - Constructor with parameters");
    }

    static {
        System.out.println("4 - Static initalizer block");
    }

    public static void main(String... args) {
        System.out.println("5 - Main method");
        new MyClass(0);
    }
}

答案

You can't use statements in the middle of your class. It have to be either in a block or in the same line as your declaration.


The usual ways to do what you want are those :

  • The initialization during the declaration

    public class MyClass{
        private int i = 0;
    }
    

    Usually it's a good idea if you want to define the default value for your field.

  • The initialization in the constructor block

    public class MyClass{
        private int i;
        public MyClass(){
            this.i = 0;
        }
    }
    

    This block can be used if you want to have some logic (if/loops) during the initialization of your field. The problem with it is that either your constructors will call one each other, or they'll all have basically the same content.
    In your case I think this is the best way to go.

  • The initialization in a method block

    public class MyClass{
        private int i;
        public void setI(int i){
            this.i = i;
        }
    }
    

    It's not really an initialization but you can set your value whenever you want.

  • The initialization in an instance initializer block

    public class MyClass{
        private int i;
        {
             i = 0;
        }
    }
    

    This way is used when the constructor isn't enough (see comments on the constructor block) but usually developers tend to avoid this form.


Resources :

On the same topic :


Bonus :

What does this code ?

public class MyClass {
    public MyClass() {
        System.out.println("1 - Constructor with no parameters");
    }

    {
        System.out.println("2 - Initializer block");
    }

    public MyClass(int i) {
        this();
        System.out.println("3 - Constructor with parameters");
    }

    static {
        System.out.println("4 - Static initalizer block");
    }

    public static void main(String... args) {
        System.out.println("5 - Main method");
        new MyClass(0);
    }
}

The answer

拥有 2024-09-25 13:49:55

将其放入初始化块中。

public class Init { 

  int instanceInt;  
  {
    instanceInt = 100;
  }

  public static void main(String[] args) {
    int localInt;
    int u = 9000;
  }   
}  

或者简单地在声明时进行初始化:

public class Init { 

  int instanceInt = 100;

  public static void main(String[] args) {
    int localInt;
    int u = 9000;
  }   
} 

参考:

Put it in an initializer block.

public class Init { 

  int instanceInt;  
  {
    instanceInt = 100;
  }

  public static void main(String[] args) {
    int localInt;
    int u = 9000;
  }   
}  

Or simply initialize while declaring it:

public class Init { 

  int instanceInt = 100;

  public static void main(String[] args) {
    int localInt;
    int u = 9000;
  }   
} 

References:

夏の忆 2024-09-25 13:49:55

您不能使用单独的语句来为声明区域中的类成员赋值。您必须通过函数(通常是类方法)来执行此操作。

对于您的情况,请考虑在您宣布后立即进行作业。

You can not have a separate statement to assign values to class members in the declaration area. You have to do this from a function, typically a class method.

For your case, consider doing the assignment as soon as you declare.

用心笑 2024-09-25 13:49:55

您需要做的是:

int instanceInt = 100;

如果它是静态的,您可以在静态块中初始化。

You need to do :

int instanceInt = 100;

If it was static you could initialize in a static block.

囚我心虐我身 2024-09-25 13:49:55

根据 Java 语言规范

类体可以包含声明
类的成员,即
字段(§8.3),类(§8.5),
接口(第 8.5 节)和方法(第 8.4 节)。
类体还可以包含实例
初始化器(第 8.6 节),静态
初始化器(第 8.7 节)和声明
类的构造函数(第 8.8 节)。

然而,该语句

instanceInt = 100;

不是这些东西,因此不允许它作为类主体的一部分。您需要做的是:

int instanceInt = 100;

这是允许的,因为它是一个包含变量初始值设定项的字段声明

According to the Java Language Specification:

A class body may contain declarations
of members of the class, that is,
fields (§8.3), classes (§8.5),
interfaces (§8.5) and methods (§8.4).
A class body may also contain instance
initializers (§8.6), static
initializers (§8.7), and declarations
of constructors (§8.8) for the class.

However, the statement

instanceInt = 100;

is none of those things, therefore it is not allowed as part of a class body. What you need to do is this:

int instanceInt = 100;

This is allowed because it is a field declaration that includes a variable initializer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文