'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 declartion and Method above above??



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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.

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gigadot
Solution 2
Solution 3 Dominik Seibold
Solution 4 KCoder
Solution 5