Struggling to understand the effects of access modifiers and the keyword "static"

Viewed 65

I'm pretty new to Java. I've just written a small example to observe the effects of access modifiers. Maybe the code does not make much sense since I'm just testing things out.

The first code snippet looks like this:

class Tet {
    static double a;
    double b;
    public Tet(double a, double b){
        this.a=a;
        this.b=b;
    }
    
    public void get(){
        a=5;
        
    }
    
    public static void main(String[] args){
        Tet tet1 = new Tet(2, 5);
        Tet tet2 = new Tet(4, 5);
        System.out.println(tet1.a);
        a=4;
        System.out.println(tet1.a);
        tet1.get();
        System.out.println(tet1.a);
        System.out.println(a);
        
    }
}

After this, I made some changes to the code and compiled it again:

class Tet {
    static double a;
    double b;
    public Tet(double a, double b){
        this.a=a;
        this.b=b;
    }
    
    public static void get(){
        a=5;
        
    }
    
    public static void main(String[] args){
        Tet tet1 = new Tet(2, 5);
        Tet tet2 = new Tet(4, 5);
        System.out.println(a);
        get();
        System.out.println(tet1.a);
        System.out.println(a);
        System.out.println(tet2.a);
        
    }
}

After it runs, the cmd looks like this: enter image description here

I really don't know where is number 4 coming from.

4 Answers

There is only one instance of static fields for each class.

In this case there is one instance of a. So when you create the tet2 instance Tet tet2 = new Tet(4, 5);, you just set its value to 4.

I can see how these things can be confusing, so no need to apologise.

Firstly, with static members, you shouldn't be using the this keyword as a static member belongs to a class itself and not an instance, i.e. objects of that class. Or put in a clearer way:

In the Java programming language, the keyword static means that the particular member belongs to a type itself, rather than to an instance of that type.

This means we'll create only one instance of that static member that is shared across all instances of the class.

So change your constructor:

public Tet(double a, double b){
    Tet.a =a;
    this.b=b;
}

You can even change the client code.

Furthermore, I would also recommend you watch this high-level video on Java memory management: https://youtu.be/4yKxJjYXZ0A or read this article by Oracle.

Access modifiers are more of a general concept in Java and aren't pertinent to a discussion around static members. Basically, it pertains to the visibility of a member, e.g. classes, variables, etc. Please read this article on the matter.

To answer your question, the "number 4" comes from Tet tet2 = new Tet(4, 5); because through the constructor, you're still updating the value of the static variable despite not creating a new object of it; it'll still have the same memory address, but its value can change as they aren't the same. And you change it again to "5" by invoking the get(); method.

I hope this clarifies a few things, and let me know if I've made any mistakes in this answer.

  1. All Java's variables must be known at compile time unlike Python, in either way:
    1. declare local variable (in this case, declare int a in your main)
    2. qualify variable (in this case, change to Tet.a)
    3. (import variable, but it's a bit longer and redundant this case)
  2. You're touching static area - an special storage. static variables are shared across all class instances. So:
public static void main(String[] args){
    Tet tet1 = new Tet(2, 5); // set Tet's `a` to 2
    // update `a` to 4.
    // `a` is shared across `tet1` and `tet2`!
    Tet tet2 = new Tet(4, 5);
    // Watch out, `Tet`'s `a` is shared! It'll output 4
    System.out.println(tet1.a); 
    System.out.println(tet1.a);
    tet1.get(); // `get`, but updates `a` to 5
    System.out.println(tet1.a); // This will be 5 as well
}

If this answer resolves your question, consider mark this as "resolved" :)

to understand the meaning of keyword called static please refer to this small tutorial to understand the meaning of static. so when you coded the following 2 lines :

    Tet tet1 = new Tet(2, 5);
    Tet tet2 = new Tet(4, 5);

this made 2 instances of the class called Tet but the variable called a is static means it's shared among the 2 instances.

so when you write Tet tet1 = new Tet(2, 5); it made this diagram as a brief representation :

enter image description here

but then when wrote Tet tet2 = new Tet(4, 5); the following diagram happened and a was updated with the new value as it's shared :

enter image description here

so both instances of Tet have that variable called a which is shared meaning if you updated a using any of the 2 instance , it's updated in the second instance also.

also in C language, making the variable static means that it's only one instance and also private to the file meaning you can't extern that variable to any other file

Related