简体   繁体   English

Java中的基本Java Question,变量类型和方法类型

[英]Basic Java Question, variable type & Method type in java

I have very very basic java question: For the class below: 我有一个非常基本的Java问题:对于下面的类:

public class Hello {
    public final static int a;
    public final int a;
    public int a;
    int a;

    static public void Method(){}
    public void Method(){}
    private void Method(){}
}

what is the difference between the declartion and Method above above?? 上面的声明和方法之间有什么区别?

public final static int a;

This declares a constant property that is static. 这声明了一个恒定的静态属性。 That means it is not tied to any instance of the Hello class and is accessible both outside the Hello class and inside. 这意味着它没有绑定到Hello类的任何实例,并且可以在Hello类的内部和内部访问。 Since it is final you will not be able to modify this value and it will always be its default value of 0. 由于是最终版本,您将无法修改此值,并且它将始终是其默认值0。

public final int a;

Similar to the one above, except it is tied to this instance of Hello. 与上面的示例类似,不同之处在于它与Hello实例绑定在一起。

public int a;

This is a public property of this instance of Hello, modifiable both inside and outside the Hello class. 这是Hello实例的公共属性,可以在Hello类的内部和外部进行修改。 This is considered bad practice. 这被认为是不良做法。

int a;

A default level property of this instance of Hello. Hello实例的默认级别属性。 Modifiable only inside this instance of Hello. 仅在此Hello实例内部可修改。

static public void Method() {}

A static (not tied to an instance) method within Hello class that is accessible both internally and externally. Hello类中的一个静态(不绑定到实例)方法,可以在内部和外部访问。 It does not have access to things like: 它无法访问以下内容:

public final int a;  
public int a;
int a;

It cannot access these because this method is not tied to this instance where as all of those values are. 它无法访问它们,因为此方法未绑定到所有这些值都在其中的实例。

public void Method() {}

A public method of Hello class. Hello类的公共方法。 It is accessible both internally and externally. 内部和外部均可访问。

private void Method() {}

Similar to the method above, except it is only accessible internally. 与上面的方法类似,除了只能在内部访问。

Corey Sunwold's answer is very clear. 科里·桑沃尔德(Corey Sunwold)的答案很明确。 I just want to add a few words if you don't already know. 如果您还不知道,我只想补充几句话。 if final is used for object reference, it means you cannot change it to reference another object but the object itself is still mutable. 如果将final用于对象引用,则意味着您不能将其更改为引用另一个对象,但是对象本身仍然可变。 For example 例如

public final static List a = new ArrayList();

The variable a is set to reference an instance of ArrayList . 变量a设置为引用ArrayList的实例。 You will not be able to set a to another ArrayList object but you can still add element to a . 您将不能设置a到另一个ArrayList对象,但你仍然可以添加元素a

final keyword in Java does not mean constant and is not equivalent to const keyword in C++. Java中的final关键字并不意味着常量,并且不等同于C ++中的const关键字。 It really does mean that variables (primitive), references (objects), methods or classes are final and cannot be further modified. 这确实意味着变量(原始),引用(对象),方法或类是最终的,无法进一步修改。

public final static int a;

it does not belong to any instance and remains constant. 它不属于任何实例,并且保持不变。

public final int a;

It is constant and cannot be changed throughout the program/Application 它是恒定的,不能在整个程序/应用程序中更改

public int a;

Normal declaration of variable, Can be changed at any point of time through the application/program. 变量的常规声明,可以在任何时间通过应用程序/程序进行更改。

int a;

Same as public int a, the variables declared without a specifiers remains public by default. 与public int a相同,默认情况下,不带说明符声明的变量仍保持public。 so, there will be no difference 因此,没有区别

static public void Method(){}

This method does not belong to any instance of a class. 此方法不属于类的任何实例。

public void Method(){}

This method can be accessed by any other class by extending, ie with the help of inheritance. 任何其他类都可以通过扩展(即在继承的帮助下)访问此方法。

private void Method(){}

This method can be accessed only by the class where it belongs to. 该方法只能由其所属的类访问。 hence cannot be accessed by other class at any point. 因此其他类无法随时访问。

For More about access specifiers click here For More about Inheritance click here 有关访问说明的更多信息, 请单击此处 。有关继承的更多信息, 请单击此处。

Let's do your homework: 让我们做功课:

public final static int a;

a is public, so it's accessible from everywhere. a是公开的,因此可以从任何地方访问。 It's final, so its value can't be changed after its declaration. 它是最终的,因此在声明后不能更改其值。 It's static, so it doesn't belong to instances of the Hello -class, but to the class itself. 它是静态的,因此它不属于Hello -class的实例,而是属于类本身。

public class Hello {
    public final static int a;
    public final int a;
    public int a;
    int a;

    static public void Method(){}
    public void Method(){}
    private void Method(){}
}

what is the difference between the declartion and Method above above?? 上面的声明和方法之间有什么区别?
=> Declarations are first 4 lines because you did not assign anything into it. =>声明是前4行,因为您没有在其中声明任何内容。 If you assign with = (ex. int a = 3), then it is called as an assignment statement. 如果使用= (例如int a = 3)进行赋值,则将其称为赋值语句。
And last three lines are methods. 最后三行是方法。 They have format as : 它们的格式为:
access modifier(ex. public) return type(ex. void) method name(ex.Method) parameter( () ) body ( {} ) 访问修饰符(例如,公共)返回类型(例如,无效)方法名称(例如,方法)参数(())主体({})


Difference between declaration and method is : 声明和方法之间的区别是:
- declaration : You declare something with name that you will you, but you have not assigned anything into it yet. -声明:您声明了一个您愿意的名称,但尚未分配任何内容。
- method : A function which executes the code inside the body. -方法:在体内执行代码的函数

Here two things are tricky, First: 这里有两件事很棘手,第一:

public final static int a; 

Actually this is static variable means (not tied to the instance) then its final so its mean value should be constant throughout the program and also public that's mean its accessible internally and externally. 实际上,这是静态变量,而不是与实例相关联,因此它是最终变量,因此它的平均值在整个程序中应该是恒定的,并且在所有程序中都是公共的,这意味着可以在内部和外部访问它。

Second: 第二:

static public void Method() {}

Same with this method not ties to the instance and accessible internally and externally 与此方法相同,不依赖于实例并且可以在内部和外部访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM