-2

Is there even any difference? Are they just different terms? I need to be able to explain the difference on school and what i wrote below seems like what the internet describes as a global variable.

Here's my code:

public class variableTypes

{
    public static String a = "hello!"; //<--- global variable (i think)

public static void main(String[]args)
{
  System.out.println(variableTypes.a);
}
Thomas Linssen
  • 129
  • 3
  • 9
  • Possible duplicate of [Local VS global variables in Java](https://stackoverflow.com/questions/21858226/local-vs-global-variables-in-java) – Lino Nov 10 '17 at 10:13
  • 4
    for one: Java doesn't have global variables – Stultuske Nov 10 '17 at 10:13
  • just copy pasted you're title into google and voilà. every answer you need – Lino Nov 10 '17 at 10:14
  • Possible duplicate of [What does the 'static' keyword do in a class?](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – OH GOD SPIDERS Nov 10 '17 at 10:21

2 Answers2

1

Something that is static can be accessed by any class or method in the same namespace. Static variables will persist for the lifetime of the program.

Something that is local to a class, will only exist for the lifetime of an instance of that class (an object), and can only be accessed through the instance.

Something that is local to a function/method, will only exist for the execution of that method.

It's the difference between:

  • i = Class.variable
  • i = myClass.variable
  • i = myClass.Function() if the function was something simple like int apple = 4; return apple otherwise, there isn't really a way to access variables local to a method/function.

Note: I've ignored access modifiers, but let's just say this holds true given everything is public. Otherwise, every thing I say only mostly holds true with some extra work. (Probably better practice too.)

Disclamer: I've used Java before, but it's not something I use regularly. But the concepts are pretty standard.

Disclamer: I'm a student too, I can be wrong, but I think I'm right.

I found this site, which seems to kinda back up what I'm saying: https://www.guru99.com/java-static-variable-methods.html

The relevant-ish part in case the site goes offline:

Java static variable It is a variable which belongs to the class and not to object(instance) Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables A single copy to be shared by all instances of the class A static variable can be accessed directly by the class name and doesn’t need any object Syntax : .

Good luck!

Gry-
  • 176
  • 9
0
class A {
    static String text = "Manoj";

    static void print() {
        String stack = "Stack";
        System.out.println(stack);// local variable
        System.out.println(A.text);// global
    }

    public static void main(String... args) {
        print();
        System.out.println(stack);// will give error because scope is local to print method
        System.out.println(A.text);// global so will work anywhere in class
    }
}
Manoj
  • 1
  • misleading. text is a class-level variable, not a global variable. In Java, it is not possible to have a variable that is not linked to a class/interface, which is a pre-requisite for it to be "global" – Stultuske Nov 10 '17 at 10:48
  • There is no direct concept of global variable in java, but you implement the same in different number of ways like below. public static String text = "Manoj"; – Manoj Nov 10 '17 at 11:10