Ronan Venkat
Ronan Venkat

Reputation: 355

Accessing variables from multiple methods in java

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?

Upvotes: 0

Views: 1275

Answers (3)

Gabriel H
Gabriel H

Reputation: 1576

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

Upvotes: 0

Avinash Sagar
Avinash Sagar

Reputation: 517

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.

Upvotes: 0

user7655213
user7655213

Reputation:

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.

Upvotes: 3

Related Questions