Java类名作为变量

huangapple go评论137阅读模式
英文:

Java Class Name As Variable

问题

package OOP_in_java;

public class Number {

public static int numberAdd(int number1, int number2) {
    return number1 + number2;
}

public static int numberMult(int number1, int number2) {
    return number1 * number2;
}

}

class custom_class {
//static Number n;
public static void main(String[] args) {
    Number n = new Number();
    System.out.println(n.numberMult(10, 10));
}

}
Can anyone explain the difference between the commented code difference 

static Number n;
Vs

Number n = new Number();

They both do the same job so Why one would use above other?
英文:
package OOP_in_java;

public class Number {

public static int numberAdd(int number1, int number2) {
    return number1 + number2;
}

public static int numberMult(int number1, int number2) {
    return number1 * number2;
}

}
//Please uncomment anyone of commented code for the program to work.
class custom_class {
//static Number n;
public static void main(String[] args) {
    //Number n = new Number();
    System.out.println(n.numberMult(10, 10));
}

}

Can anyone explain the difference between the commented code difference

static Number n;

Vs

Number n = new Number();

They both do the same job so Why one would use above other?

答案1

得分: 1

static Number n;

在类`custom_class`中创建一个静态变量其他方法可以通过该类访问您当前没有其他方法所以与您无关什么时候会使用这个也许您有一个名为`mySpecialNumber`的变量您将在整个类中使用它您可以有不同的方法使用它一个方法可以将参数与它相乘另一个方法可以将参数添加到它这将是一个在整个类中都可以访问的全局变量

由于它是静态的您无需实例化类即可使用它它是一个独立的变量您无需创建类的对象即可使用它在您的情况下您可以在不创建`Number`类实例的情况下使用`n`。这可能提供更多的灵活性但是它不太安全可能会导致数据泄漏

Number n = new Number();

在运行时创建`Number`对象的实例变量它只能在`main`方法中访问如果在这个类中有其他方法其他方法将无法访问`n`的值一旦在方法内部声明了一个变量它就在该变量的局部范围内只有同一方法中的代码行才能引用它您的主方法可以询问用户的年龄并将其存储在主方法中的局部变量中

通过这种方法您正在实例化Number类这一行代码会自动调用Number类的构造函数并创建一个赋值给`n`的Number类型的对象在您的代码中您没有指定构造函数因此将使用默认构造函数`n`所做的任何更改只会影响当前运行时它们不会传递到程序的未来运行该对象在主方法中创建因此只能在主方法中访问
英文:
static Number n;

creates a static variable in the class custom_class that can be accessed by other methods in that class. You currently don't have any other methods so it's not relevant to you. When would this be used? Perhaps you have a variable mySpecialNumber that you will use throughout the class. You can have different methods that use it. One method may multiple the parameter with it. Another method may add the parameter to it. This is going to be a global variable that is accessible throughout the class.

Since it is static, you do not need to instantiate the class in order to use it. It is a variable that stands alone. You do not need to create an object of the class in order to use it. In your case, you can use n without creating an instance of the Number class. This might provide more flexibility. However, it is less secure and can cause data breaches.

Number n = new Number();

creates an instance variable of the Number object during runtime. It is only accessible within the main method. If you had other methods in this class, other methods would not be able to access the value of n. Once a variable is declared inside a method, it has a local scope to that variable. Only lines of code in the same method can refer to it. Your main method could as users for their age and store it in a local variable in the main method.

With this methodology, you are instantiating the Number class. This line automatically calls the constructor of the Number class and creates an object of type Number which gets assigned to n. In your code you did not specify a constructor so the default constructor would be used. Any changes made to n only effect the current runtime. They do not transmit to future runs of the program. The object is created in the main method and therefore only accessible in the main method.

答案2

得分: 1

他们不做相同的事情。

static Number n; 声明了一个静态变量,但从未实例化它。nnull

Number n = new Number(); 声明了一个局部变量 n,并赋予了实际值。

英文:

They don't do the same thing.

static Number n; declares a static variable, but never instantiates it. n is null.

Number n = new Number(); declares a local variable n and gives it an actual value.

huangapple
  • 本文由 发表于 2020年4月6日 08:43:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61051370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定