0

I'm starting to learn java, and I don't fully understand class variables and instance variables. I've seen some StackOverflow posts about accessing a variable from multiple methods, and most of the answers say to use static class variables. My question is, why can't one just use private instance variables? What are the advantages of using one over the other?

1
  • 1
    "most of the answers say to use static class variables" - I really doubt it. On the contrary, you should use static variables as little as possible. Commented Jul 20, 2020 at 20:46

3 Answers 3

3

Instance variables are only accessible from instance functions (i. e. from non-static functions). Under the OOP paradigm, most of your variables will likely be instance variables.

Class variables (static) can also be accessed from class functions (static), like your main function.

Say, you have a class called Car. Every instance of this class (say, myFerrari and myBeetle) has its own variables doors, engine, gearbox et cetera. Thus, they are non-static, as are functions like drive (because you are driving an individual car, not the concept of cars).

But you might have a static variable MAX_ALLOWED_PASSENGERS which tells you how many seats a car may have before it's considered a bus instead. This is tied to the concept of cars (i. e. the class), not to any individual car. Thus, it might be static.

As a beginner, depending on your method of learning, you may not yet understand OOP and only write procedural code - which is not what Java is designed for. Thus you may encounter weirdnesses, like code examples where all functions and variables are static. This is a side effect of using Java in an unintended way and its purpose will be clear to you later on.

Sign up to request clarification or add additional context in comments.

Comments

0

You can think of a static variable as a shared variable between all instances of the class. It is mostly used when you have a need of common value to be accessible across all objects of the class. Look at below example and execute it. It will clarify things. Please note that I have accessed a static variable with instance which is not the right practice but it is for demonstration purpose only.

public class A{
    static int counter=1;
    private int value;
    
    public A(int value){
        this.value = value;
    }
    
    public int getValue(){
        return this.value;
    }
}

public class B{
    public static void main(String[] args){
        A a1 = new A(10);
        A a2 = new A(12);
        System.out.println(a1.value);
        System.out.println(a2.value);
        
        System.out.println(A.counter);
        System.out.println(a1.counter);
        System.out.println(a2.counter);
        
        A.counter=20;
        System.out.println(A.counter);
        System.out.println(a1.counter);
        System.out.println(a2.counter);
    }
}

Notice how changing value of counter in class A impacts the result for both variables.

2 Comments

Saying that a static variable is shared between all instances of a class is somewhat misleading, because you do not need an instance of the class in order to access static variables, and the static variable exists previous to any instantiation.
Yes, that's right but since the user is a beginner, I have tried to keep it simple. It might not seem good but I suppose some people need easy description to understand at first and the perspective keeps changing when they will really use these concepts in solving real world problems.
0

Ok first of all you should in most cases use instance variable.

class variables (static variable) are used when you need one instance across the whole class.

meaning if you have 2 different objects the static variable will be used for both of them, while using instance variable means that each object will hold its own vriable with its own value.

example:

public class Test{

public static int classVar=1;
public int instanceVar;
}

now this code will yield different resulkt based on the variable used:

Test o1=new Test();
o1.instanceVar=1;

Test o2=new Test();
o2.instanceVar=2;

System.out.println("class var object 1"+o1.classVar);//will result in 1 and warn that shoud be used as class level there for as Test.classVar
System.out.println("instance var object 1"+o1.classVar);//will result in 1

System.out.println("class var object 1"+o2.classVar);//will result in 1
System.out.println("instance var object 1"+o2.classVar);//will result in 2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.