有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

定义Java中静态变量和方法的概念

我一直在拼命寻找清晰的答案,我想我有点明白了,但同时我不太明白这个关键词的广义概念,静态

以下是我的设想:

package oops;

public class Math {

boolean notNumber = false;
static boolean notString = false;

public static void main(String[] args) {

    int num1 = 1;
    static int num2 = 1; //doesn't work

    Math math = new Math();
    math.notNumber = true;

    notNumber = true; //doesn't work
    notString = true;
}

public void whatever() {
    notNumber = true;
}

}
  1. 为什么不能在静态方法(或任何方法)中声明一个静态变量?“范围”是什么意思?我知道静态变量与类的特定实例没有关联,它更像是一个“全局”变量。但是为什么不能在方法内部创建一个静态变量(num2),而可以使用静态变量(notString)

  2. 当您创建静态变量时,是否必须在类中创建它们?在方法上不可能

  3. 因为我声明notNumber为非静态,所以我知道必须创建一个对象来访问该变量。但是为什么它在whatever()中工作而不创建任何对象,而在静态方法main()中却不工作呢


共 (5) 个答案

  1. # 1 楼答案

    让我们逐一回答。但在此之前,让我们了解什么是静态变量,什么是非静态变量。静态变量是属于类的变量,而不是属于类的任何特定实例,而非静态变量则属于类实例

    请考虑下面的例子

    class A{
    static Object someStaticValue;
    Object someNonStaticValue;
    }
    
    A.someStaticValue = new Object(); //allowed
    A.someNonStaticValue = new Object() //not allowed as someNonStaticValue belongs to instance of A and will be different for each instance of A
    A objectA = new A();
    objectA.someNonStaticValue = new Object(); //allowed as this will update someNonStaticValue which is in scope of objectA
    
    objectA.someStaticValue = new Object(); //allowed as it will simply update the value of variable in Class scope.
    
    A objectB = new A();
    objectB.someNonStaticValue = new Object(); //allowed
    objectB.someStaticValue = new Object(); //allowed as it will simply update the value of variable in Class scope.
    

    objectA.someNonStaticValue is not equal to objectB.someNonStaticValue as they belong to two different scope

    whereas A.someStaticValue, objectA.someStaticValue, objectB.someStaticValue -> All are equal as they are updating the variable in class scope.

    现在来回答你们的问题:

    Why can't you declare a static variable in a static or non-static method?

    在方法中声明的任何变量都将在方法中具有作用域,因此永远不能属于类。每次调用该方法时,都会使用一个新实例,因此每次调用都会有所不同,因此没有在方法中创建静态变量的目的

    When you make static variables, do you HAVE TO make them in the class? Not possible in the methods?

    是的,因为在方法中创建将无法解决任何问题,并且将无法保持静态的定义,因为值将在对该方法的不同调用中更改

    Since I declared notNumber as non-static, I know I have to create an object to access that variable. But why does it work in whatever() without any creation of objects, but not in the static method main()?

    因为在which()函数中,可以确定您已经创建了一个类的新实例,您使用该实例调用了该函数,因此允许在非静态变量中调用非静态变量。此外,在这种情况下,JVM很清楚该变量使用哪个实例范围。然而,对于静态方法,不能保证类的实例是否已创建,也不能保证该非静态变量将使用哪个类实例范围。因此,不允许从静态方法调用非静态变量

  2. # 2 楼答案

    为什么不能在static方法(或任何方法)中将变量声明为static?“范围”是什么意思?我知道静态变量与类的特定实例没有关联,它更像是一个“全局”变量。但是为什么不能在方法内部创建一个静态变量(num2),而可以使用静态变量(notString)

    答案:在方法中不能将varibale声明为静态。 在方法内部,所有变量都是局部变量,在方法外部不存在,这就是为什么它们不能是静态的

    但为什么它在任何情况下都有效()

    回答:因为 boolean notNumber是在类级别声明的,任何方法都是非静态的,这就是为什么。不能在static中引用非static

    为什么不能在STATIC中引用非STATIC Why non-static variable cannot be reference from a static context - reg

    当你制作静态变量时,你必须在课堂上制作它们吗?在方法上不可能

    回答不能在静态方法中生成静态变量参见第一个答案

  3. # 3 楼答案

    But why can't you create a static variable inside the method (num2), but can USE the static variable (notString)?

    When you make static variables, do you HAVE TO make them in the class? Not possible in the methods?

    static的作用域是类上下文。因此,在方法作用域中声明static变量毫无意义
    这就像在方法中声明实例字段一样
    声明一个变量和使用它是两件截然不同的事情,它们不遵循相同的规则
    根据经验,您可以在适合该修饰符的位置声明带有特定修饰符的变量。例如:

    • 类顶级的实例和静态字段
    • 方法层面的局部变量

    在任何与该修饰符兼容的上下文中,使用带有特定修饰符的变量:

    • 实例上下文中的实例变量
    • 实例和静态上下文中的静态变量

    Since I declared notNumber as non-static, I know I have to create an object to access that variable. But why does it work in whatever() without any creation of objects, but not in the static method main()?

    这是一个实例方法:

    public void whatever() {
        notNumber = true;
    }
    

    因此,访问类的实例成员是有效的

    而另一个是static方法。因此,它可以引用static字段,但不能引用实例字段:

    public class Math {
    
       boolean notNumber = false; // instance
       static boolean notString = false; // static
    
        public static void main(String[] args) {
    
            ...    
            notNumber = true; //doesn't work as refers an instance field
            notString = true; // work as refers a static field
        }
      ...
     }
    
  4. # 4 楼答案

    因为绝对不需要在方法中声明static viriablesstatic variables是该类的所有实例共享的成员。假设允许在main方法中声明它,并且您有另一个静态方法foo,那么如何在foo中访问该变量

  5. # 5 楼答案

    1. 您似乎理解静态成员不属于任何实例。它们属于本身。它们并不像你说的那样是“全球性的”。当您想从另一个类访问类名时,仍然需要先写类名。因为静态成员属于类,所以它们存在的唯一合理位置是类级别。你不能在方法中声明它们,因为这会让它们看起来像是属于方法(比如,它们的作用域是方法体),而它们不是。你可以在同一类的方法中使用它们,而不必限定类名,因为方法在类的作用域中

    2. 如上所述,他们必须在课堂上

    3. 这是个好问题。如果你仔细观察,whatever也不是静态的!这意味着您必须先有一个实例,然后才能执行whatever。这个实例在whatever中被称为“this”。你对notNumber的使用是this.notNumber的缩写。您正在使用调用whatever的实例的notNumber字段