开发者

Basic Java Question, variable type & Method type in java

I have very very basic java question: For the class below:

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开发者_StackOverflow社区 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. Since it is final you will not be able to modify this value and it will always be its default value of 0.

public final int a;

Similar to the one above, except it is tied to this instance of Hello.

public int a;

This is a public property of this instance of Hello, modifiable both inside and outside the Hello class. This is considered bad practice.

int a;

A default level property of this instance of Hello. Modifiable only inside this instance of Hello.

static public void Method() {}

A static (not tied to an instance) method within Hello class that is accessible both internally and externally. 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. 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. 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. For example

public final static List a = new ArrayList();

The variable a is set to reference an instance of ArrayList. You will not be able to set a to another ArrayList object but you can still add element to a.

final keyword in Java does not mean constant and is not equivalent to const keyword in C++. 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. 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, i.e. 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. 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.


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. If you assign with = (ex. int a = 3), then it is called as an assignment statement.
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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜