Question

Is there any particular relation between class instance (e.g variables that declared by static/final) and static methods(e.g class methods) ?

I hop you understand what i mean.

Was it helpful?

Solution 2

Static and non-static methods/classes are entirely different beasts. Let's use an example of object-oriented programming. Say I have a class called "Person", with a constructor called Person(), with the instance variables declared as myAge (set as 0), and a method called Birthday():

int myAge;

public Person(){
    myAge = 0;
}

public void Birthday(){
    myAge += 1;
}

In my main method, this class would be used like this:

Person Sajjad = new Person();
Sajjad.Birthday();

When I create a new person, which is you, your age is 0, because our constructor's default values are 0 for myAge. Once I apply the Birthday() method on you, your age goes up by one, through the increment.

As for static methods, there's no object-oriented principles in it. There are uses for it.. I usually use it for simple math.

public static double addValues(double a, double b){
    return a + b;
}

In my main:

int sum;
sum = addValues(1, 2);
System.out.println(sum);

Output:

3

See how above there's no need to declare a new objects? Static methods are easier to prototype, but in the long run I definitely go with object-oriented principles because it makes maintaining code in the long run so much easier. Also, I don't need to clutter my main method with unnecessary lines of code.

P.S. If the code is wrong, it's really just some pseudo code I whipped up.

OTHER TIPS

I think what you talk about is static variable instead of class instance (no this term), one association between static variable and static method I can think of is that, in your static method you can only access static variables rather than instance variables.

Class instances are objects stored in memory and referenced through a variable of the Type of the class. Each element has its own state and its own methods. Ex: if we have a Apple class and a getColor non-static element:

Apple a = new Apple("green");
Apple b = new Apple("red");
a.getColor();//green
b.getColor();//red

Static methods/variables are elements related to that class and acceded via the name of the class. You can't access static elements from an instance. Ex: if we have a Apple class and a size static element:

Apple.tree; //Apple Tree

Here's class Apple

class Apple {
    public static String tree = "Apple Tree"; // Class related
    private String color; //object instance related

    public Apple(String color) {
        this.color = color;
    }

    public String getColor() {
        return this.color;
    }
}

So, having this class color will depend on the instance.

Access: class instance (i.e variables that declared by static/final) and static methods can work together without explicit need of object reference. Whereas to access member variable (aka instance variables) you need to access via object reference.

class Test {
    static String str = "asdf";
    int num = 10;

    static void methodA() {
        System.out.println("Static variable direct access: " + str);
        System.out.println("Member variable access : " + new Test().num);
    }
}

Concurrency: Other difference is during concurrent access or thread-safety logic, class-level-lock can be taken via class-variables in static-methods and object-level-lock can be taken via member-variables in member-methods though you can mix-n-match ofcourse.

class Test {
    static Object CLASS_LOCK = new Object();
    Object MEMBER_LOCK = new Object();
    Test sharedTest = new Test(); // or created in different manner

    static void methodA() {
        synchronized (CLASS_LOCK) {
            // .. thread-safe code
        }

        synchronized (sharedTest.MEMBER_LOCK) {
            // .. thread-safe code
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top