How to define Global variable outside class?
java
Solution
You can declare it as a static value in your class:
public class Main {
public static int sharedValue = 100;
....
}
and access it from other classes using:
Main.sharedValue
Problem
I'd like to define variable which can share values to classes. so I tried as following. But It occured error. How to share a value to classes? ``` package com.company; /////// Error occurred /////// int sharedValue = 100; // <- How to share to classes? ////////////////////////////// public class Main { public static void main(String[] args) { sharedValue += 10; GlobalTest globalTest = new GlobalTest(); globalTest.printGlobalValue(); } } class GlobalTest { void printGlobalValue() { System.out.println(sharedValue); } } ```