英文:
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;
声明了一个静态变量,但从未实例化它。n
为 null
。
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论